我正在为我的应用程序使用laravel和vuejs,每个用户都有一个角色。现在,我想通过将用户重定向到另一个路由(如果他们无法访问页面)来限制用户按其角色访问vue路由。我正在为vuejs使用beforeRouteEnter,但出现了一个错误
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