vue动态添加路由后刷新页面白屏问题
页面刷新白屏其实是因为vuex引起的,由于刷新页面vuex数据会丢失,所以动态添加路由这一步也就失效了
在通过vuex中的state属性存储路由在sessionStorage中,只是存储了左侧菜单栏展示的信息path地址,但是并没有存进去路由详细信息,比如name,meta值,利用vue插件即可看到本地route里面的真实数据!所以;只需要添加路由守卫,在每次页面刷新的时候都来判断是否添加的动态路由【这里设置一个变量记录是否添加的布尔值】,如果为false,即再次执行一遍添加动态路由的过程即可next() 在store.js中的mutations中添加动态路由
util.js 文件:
import Vue from vue;
let util = {
title:function (title) {
title = title ? title + | 书籍管理 : 书籍管;
window.document.title = title;
},
S4:function () {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
},
//产生唯一ID
guid:function () {
return (util.S4()+util.S4()+util.S4()+util.S4()+util.S4()+util.S4()+util.S4()+util.S4());
},
getRootPath:function () {
//获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp
let curWwwPath=window.document.location.href;
//获取主机地址之后的目录,如: uimcardprj/share/meun.jsp
let pathName=window.document.location.pathname;
let pos=curWwwPath.indexOf(pathName);
//获取主机地址,如: http://localhost:8083
return curWwwPath.substring(0,pos);
},
extendRouters:function(data) {
let child = new Array(0);
for (let item in data) {
if (data.hasOwnProperty(item)) {
if (data[item].authUrl) {
child.push(
{
path: data[item].authUrl,
component: resolve => {
require.ensure([], (require) => {
resolve(require(../components/view + data[item].authUrl + .vue))
})
}
}
);
}
if (data[item].list && data[item].list.length > 0) {
for (let childItem in data[item].list) {
if (data[item].list.hasOwnProperty(childItem) && data[item].list[childItem] && data[item].list[childItem].authUrl) {
child.push(
{
path: data[item].list[childItem].authUrl,
component: resolve => {
require.ensure([], (require) => {
resolve(require(../components/view + data[item].list[childItem].authUrl + .vue))
})
}
}
);
}
}
}
}
}
//匹配不到
child.push({path: *, component: Vue.extend({template: <div style="font-size:28px;padding:10px 20px;color:#bc7c7d;margin-top:20%;">404 page not found</div>})});
return child;
},
replaceAll:function(src,f,e) {
let reg=new RegExp(f,"g"); //创建正则RegExp对象
return src.replace(reg,e);
}
};
export default util;
