我正在尝试保护某些vue组件,以便用户必须先登录才能查看它。问题是,即使 window.laravel.islogin 为 false 并且他们尚未登录,用户仍然可以看到该组件。
App.js
const routes = [
{path:'/',component: require('./components/Home.vue').default},
{path:'/form',component: require('./components/Form.vue').default, meta: {
authRequired: true
}},
]
const router = new VueRouter({
routes,
mode: 'history'
});
router.beforeEach((to, from, next) => {
if (to.meta.authRequired) {
if(window.laravel.isLogin){
console.log(window.laravel.isLogin) //returns true or false
next()
}else{
next('/login')
}
}else {
next()
}
});
App.blade.php
<script>
window.laravel = @json(['isLogin' => auth()->check() ? 'true' : 'false']);
</script>