1.在根目录下创建middleware文件夹,在该文件夹下创建两个文件
authenticated.js【未登录跳转登录页】
export default function ({ store, redirect }) {// If the user is not authenticatedif (!store.state.user) {return redirect('/login')}}
notAuthenticated.js【已登录直接跳转首页】
export default function ({ store, redirect }) {// If the user is authenticated redirect to home pageif (store.state.user) {return redirect('/')}
}
组件中使用
export default {middleware: ['notAuthenticated'],
}