javascript - 如何让 Vue-Cookies 在 Vue-Router 组件中工作

标签 javascript cookies vue.js vuejs2 vue-router

我有一个路由器 index.js 文件,它像这样加载 Vue、Vue-Router 和 Vue-Cookies:

import Vue from 'vue';
import Router from 'vue-router';
import VueCookies from 'vue-cookies';

Vue.use(Router);
Vue.use(VueCookies);

然后我像这样定义我的所有路线:

const router = new Router({
  routes: [
    {
      path: '*',
      name: 'erorr',
      secure: false,
      component: error404,
    },
    {
      path: '/',
      name: 'Home',
      secure: false,
      component: home,
    },
    {
      path: '/clp',
      name: 'CLP',
      secure: true,
      component: clpHome,
    },
    {
      path: '/saml/login',
      name: 'samlLogin',
      secure: false,
      component: samlLogin,
    },
    {
      path: '/saml/logout',
      name: 'samlLogout',
      secure: false,
      component: samlLogout,
    },
    {
      path: '/user/profile',
      name: 'userProfile',
      secure: false,
      component: userProfile,
    },
  ],
});

在此之后,它会检查是否设置了 cookie:

router.beforeEach((to, from, next) => {
  // Look at all routes
  router.options.routes.forEach((route) => {
    // If this is the current route and it's secure
    if (((to.matched[0].path === route.path || to.matched[0].path === '')/* && route.path === '/'*/) && route.secure) {
      // Check if there's a cookie and verify it
      // Check if user has cookie "SAMLSession"

这是弹出错误的地方,“TypeError: Cannot read property 'isKey' of undefined” 当我尝试 控制台日志(这个。$cookies); 它也返回“未定义”。

      if (this.$cookies.isKey('SAMLSession')) {
        // Sets the value of "SAMLSession" cookie to a variable
        const sessionId = this.$cookies.get('SAMLSession');
        // Runs function checkSaml located above, then once that completes....
        checkSaml(sessionId).then(result => {
          // Check if the session id is valid via Express, noted by a response of "OK" if good, and "BAD!" if not valid
          if (result.data === 'OK') {
            // If it's good, allow the user to see the page
            next();
          } else {
            // If it's not valid, set a cookie of the page the user was trying to access and then sign them in
            this.$cookies.set('referLocation', to.path, Infinity, '/');
            next('/saml/login');
          }
        });
      } else {
        // If it's not a cookie, set a cookie of the page the user was trying to access and then sign them in
        this.$cookies.set('referLocation', to.path, Infinity, '/');
        next('/saml/login');
      }
    }
  });
  // If nothing has happened, allow the user to visit the page
  next();
});

这个配置在几天前工作,现在它没有加载 VueCookies,如果有任何故障排除建议,我们将不胜感激。

最佳答案

我遇到了同样的问题,试图弄清楚为什么 this 在路由器中未定义,这会导致 this.$cookies 错误。

这是我的做法。

//add this in the router index.js
import Vue from 'vue'

然后,我可以从 Vue 访问 cookie 变量

beforeEnter: (to, from, next) => {
    let access_token = Vue.cookie.get('access_token')
    if (access_token == null) {
        // user doesn't have access token, redirect to login
        next({ name: 'login' })
    }
    else {
        // user has access token, user can open the page
        next()
    }
},

关于javascript - 如何让 Vue-Cookies 在 Vue-Router 组件中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51789200/

相关文章:

javascript - 中心 Javascript 弹出到屏幕

javascript - Ajax 请求是否保证发送 cookie?

vue.js - 在初始路由之前执行 Vuex 操作

javascript - 防止在定时在线测验中作弊

javascript - 滑动页面转换,并在下方预加载新内容

asp.net - 在ASP.NET MVC C#中使用Cookie

带有 cookie 保存的 Python 登录脚本

javascript - 阻止 Mathjax 渲染

vue.js - VueJS 过滤器不工作

javascript - 切换标签时加载内容