我正在尝试将我的SPA VueJS与Laravel API一起使用。
为了处理基于角色的身份验证,我找到了插件vue-auth:
Github:https://github.com/websanova/vue-auth
文档:https://websanova.com/docs/vue-auth/home
这个插件看起来很棒,因为它看起来很适合我的需求,但我不能在我的应用程序上实现它。
import Vue from 'vue';
import auth from '@websanova/vue-auth';
import authBearer from '@websanova/vue-auth/drivers/auth/bearer.js';
import httpAxios from '@websanova/vue-auth/drivers/http/axios.1.x.js';
import routerVueRouter from '@websanova/vue-auth/dist/drivers/router/vue-router.2.x.js';
Vue.use(auth, {
auth: authBearer,
http: httpAxios,
router: routerVueRouter,
rolesKey: 'role'
});当我尝试使用我的应用程序时,我的控制台中出现以下错误消息:
Uncaught TypeError: Cannot read property 'beforeEach' of undefined
at Auth.beforeEach
at _initInterceptors
at new Auth有趣的是,当我将http驱动程序更改为vue-resource (遵循文档)时,我又遇到了另一个错误:
Uncaught TypeError: Cannot read property 'interceptors' of undefined
at Auth.interceptor
at _initInterceptors
at new Auth我已经被困了几个小时了,我在互联网上到处寻找,但都没有解决方案。
感谢您的阅读
发布于 2020-07-08 16:33:45
在我的方式中,我使用laravel身份验证方法,如JWT或Passport来处理基于角色的身份验证,您可以在路由器中使用beforeEach,例如:
router.beforeEach((to, from, next) => {
if (to.path == '/login') {
next();
} else {
store.commit('setLoading', true);
store.dispatch('checkAuth').then(r => {
store.commit('setUser', r.data);
next();
}).catch(e => next('/login?redirect=' + to.path)).finally(() => {
store.commit('setLoading', false);
})
}
})发布于 2021-11-05 02:48:53
我通过将const router = new VueRouter({ mode: 'history', base: process.env.BASE_URL, routes })放在beforeEach之前来改变这一点
https://stackoverflow.com/questions/62785950
复制相似问题