Laravel 路由之前的Vue js
我正在为我的应用程序使用laravel和vuejs,每个用户都有一个角色。现在,如果用户无法访问页面,我想通过将他们重定向到另一个路由来限制用户按其角色访问vue路由。我正在为vuejs使用beforeRouteEnter,但我得到一个错误Laravel 路由之前的Vue js,laravel,vue.js,vue-router,Laravel,Vue.js,Vue Router,我正在为我的应用程序使用laravel和vuejs,每个用户都有一个角色。现在,如果用户无法访问页面,我想通过将他们重定向到另一个路由来限制用户按其角色访问vue路由。我正在为vuejs使用beforeRouteEnter,但我得到一个错误 data() { return{ role: '', } }, mounted() { axios.post('/getRole') .then((response) => this.role = res
data() {
return{
role: '',
}
},
mounted() {
axios.post('/getRole')
.then((response) => this.role = response.data)
.catch((error) => this.errors = error.response.data.errors)
},
beforeRouteEnter (to, from, next) {
if (this.role === 'Admin') {
next()
}else {
next('/')
}
}
我得到了这个错误
app.js:72652 TypeError: Cannot read property 'role' of undefined
at beforeRouteEnter (app.js:90653)
at routeEnterGuard (app.js:72850)
at iterator (app.js:72690)
at step (app.js:72464)
at runQueue (app.js:72472)
at app.js:72726
at step (app.js:72461)
at app.js:72465
at app.js:72711
at app.js:72539
- 您的
无法在
功能中使用beforeRouteEnter
,因为在此
路由更新之前,组件的vm不存在
- 考虑到您将角色信息放入虚拟机中,您可以使用
获取虚拟机:next
- 实际上,您应该使用,并且
,这样您就可以将您的角色信息放在全局区域中
在创建目标组件之前重定向您的路由
- 您的
无法在
功能中使用beforeRouteEnter
,因为在此
路由更新之前,组件的vm不存在
- 考虑到您将角色信息放入虚拟机中,您可以使用
获取虚拟机:next
- 实际上,您应该使用,并且
,这样您就可以将您的角色信息放在全局区域中
在创建目标组件之前重定向您的路由
beforeRouteEnter(to, from, next) {
next(vm => {
// put your logic here
if (vm.role === 'Admin') {
}
})
}