2

我正在尝试将我的 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

几个小时后我就被困住了,我在互联网上到处搜索,没有任何解决方案有效。

非常感谢阅读

4

2 回答 2

0

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

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);
        })
    }
})
于 2020-07-08T08:33:44.987 回答
-2

我通过把const router = new VueRouter({ mode: 'history', base: process.env.BASE_URL, routes })之前的beforeEach

于 2021-11-05T02:48:52.573 回答