需要修改main.js文件和index.js文件:
關鍵代碼:
main.js
//在路由跳轉之前判斷,除了首頁以外,其他頁面必須登錄才能訪問,異步問題
router.beforeEach((to, from, next) => {
/* 路由發生變化修改頁面title */
if (to.meta.title) {
document.title = to.meta.title
}
if (to.path === '/' || to.path === '/login' || to.path === '/activate' || to.path === '/register') {
next();
} else {
axios.get("/user/isLogin")
.then(response => {
// console.log(response.data);
let data = response.data;
if (data.status === global.responseCode.OK && data.result === true) {
console.log("登錄成功");
next();
} else {
next('/login?redirect=' + to.path);
}
});
}
});
index.js
格式都是一樣的:
關鍵代碼:
export default new Router({
routes: [
{
path: '/',
name: 'index',
component: index,
meta: {
title: '首頁'
}
},
{
path: '/music',
name: 'Music',
component: Music,
meta:{
title:'音樂'
}
},