iview写的页面刷新返回登录页,点击页面返回登录页,3天了,没找到问题

新手上路,请多包涵

libs/util.cookies.js

import Cookies from 'js-cookie';
import Setting from '@/setting';

const cookies = {};

/**
 * @description 存储 cookie 值
 * @param {String} name cookie name
 * @param {String} value cookie value
 * @param {Object} cookieSetting cookie setting
 */
cookies.set = function(name = 'default', value = '', cookieSetting = {}) {
    let currentCookieSetting = {
        expires: Setting.cookiesExpires
    };
    Object.assign(currentCookieSetting, cookieSetting);
    Cookies.set(`${name}`, value);
};

/**
 * @description 拿到 cookie 值
 * @param {String} name cookie name
 */
cookies.get = function(name = 'default') {
    return Cookies.get(`${name}`);
};

/**
 * @description 拿到 cookie 全部的值
 */
cookies.getAll = function() {
    return Cookies.get();
};

/**
 * @description 删除 cookie
 * @param {String} name cookie name
 */
cookies.remove = function(name = 'default') {
    return Cookies.remove(`${name}`);
};

export default cookies;

router/index.js

import Vue from 'vue';
import VueRouter from 'vue-router';
import iView from 'view-design';

import util from '@/libs/util'

import Setting from '@/setting';

import store from '@/store/index';

// 路由数据
import routes from './routes';
Vue.use(VueRouter);

// 导出路由 在 main.js 里使用
const router = new VueRouter({
    base: 'UnicomHS',
    routes,
    mode: Setting.routerMode
});

/**
 * 路由拦截
 * 权限验证
 */

router.beforeEach((to, from, next) => {
    if (Setting.showProgressBar) iView.LoadingBar.start();
    // 判断是否需要登录才可以进入
    if (to.matched.some(_ => _.meta.auth)) {
        // 这里依据 token 判断是否登录,可视情况修改
        const token = util.cookies.get('token');
        // console.log(token);
        if (token && token !== 'undefined') {
            next();
        } else {
            // console.log(token);
            // 没有登录的时候跳转到登录界面
            // 携带上登陆成功之后需要跳转的页面完整路径
            next({
                name: 'login',
                query: {
                    redirect: to.fullPath
                }
            });
        }
    } else {
        // 不需要身份校验 直接通过
        next();
    }
});

router.afterEach(to => {
    if (Setting.showProgressBar) iView.LoadingBar.finish();
    // 多页控制 打开新的页面
    store.dispatch('admin/page/open', to);
    // 更改标题
    util.title({
        title: to.meta.title
    });
    // 返回页面顶端
    window.scrollTo(0, 0);
});

export default router;

爹们 还要看什么页面 我都有列表

问题描述

问题出现的环境背景及自己尝试过哪些方法

相关代码

粘贴代码文本(请勿用截图)

你期待的结果是什么?实际看到的错误信息又是什么?

阅读 2k
2 个回答

router before 里面看下 跳转逻辑有问题

router.beforeEach打印一下util.cookies.get('token'),看看是走next还是跳转login

推荐问题