首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vue-Auth :无法读取未定义的属性“”beforeEach“”

Vue-Auth :无法读取未定义的属性“”beforeEach“”
EN

Stack Overflow用户
提问于 2020-07-08 08:52:12
回答 2查看 1.2K关注 0票数 2

我正在尝试将我的SPA VueJS与Laravel API一起使用。

为了处理基于角色的身份验证,我找到了插件vue-auth:

Github:https://github.com/websanova/vue-auth

文档:https://websanova.com/docs/vue-auth/home

这个插件看起来很棒,因为它看起来很适合我的需求,但我不能在我的应用程序上实现它。

代码语言:javascript
复制
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'
});

当我尝试使用我的应用程序时,我的控制台中出现以下错误消息:

代码语言:javascript
复制
Uncaught TypeError: Cannot read property 'beforeEach' of undefined
at Auth.beforeEach
at _initInterceptors
at new Auth

有趣的是,当我将http驱动程序更改为vue-resource (遵循文档)时,我又遇到了另一个错误:

代码语言:javascript
复制
Uncaught TypeError: Cannot read property 'interceptors' of undefined
at Auth.interceptor
at _initInterceptors
at new Auth

我已经被困了几个小时了,我在互联网上到处寻找,但都没有解决方案。

感谢您的阅读

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-07-08 16:33:45

在我的方式中,我使用laravel身份验证方法,如JWT或Passport来处理基于角色的身份验证,您可以在路由器中使用beforeEach,例如:

代码语言:javascript
复制
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);
        })
    }
})
票数 0
EN

Stack Overflow用户

发布于 2021-11-05 02:48:53

我通过将const router = new VueRouter({ mode: 'history', base: process.env.BASE_URL, routes })放在beforeEach之前来改变这一点

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62785950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档