添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

vue路径上如何设置指定的前缀

作者:Lou_Lan

这篇文章主要介绍了vue路径上如何设置指定的前缀,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

vue路径上设置指定的前缀

有时在使用项目的时候,我们都需要指定一个前缀路径(就像tomcat中的虚拟路径),这个时候在vue中如何使用呢。

这个时候我们可以使用vue-router中的base这个属性,使用这个属性就可以在路径前面添加指定的前缀。

export default new Router({
  mode: 'history', //后端支持可开
  # base: '/wtlicence',
  scrollBehavior: () => ({
  routes: constantRouterMap

这个时候的访问路径是: http://127.0.0.1:8080/login.

当我们使用vue-router的base属性的时候。

export default new Router({
  mode: 'history', //后端支持可开
  base: '/wtlicence',
  scrollBehavior: () => ({
  routes: constantRouterMap

这个时候的访问路径是: http://127.0.0.1:8080/wtlicence/login

vue history模式、前缀

路由history模式

router/index.js

mode: 'history',
base: '/sss', // 路由前缀

config/index.js

开发dev和线上build配置中,将static改成想要的前缀。

  • assetsSubDirectory: 打包后的静态资源要存放的路径(static)

最后,改成history模式后部署, 刷新会有问题。需要更改服务器配置(config)

server{
	listen      8888;
	server_name  localhost;
	root html
	location / { 
	    try_files $uri $uri/ @router; 
	    index index.html index.htm; 
	location @router { 
		rewrite ^.*$ /index.html last; 

新增的主要是: