【问题标题】:get is undefined for this.$cookie.get for vuejsget 未定义 this.$cookie.get 对于 vuejs
【发布时间】:2017-07-12 12:51:11
【问题描述】:

我在路由器 index.js 中使用此代码作为 Vuejs 中的登录路由

if (this.$cookie.get('token')) {
    next('/dashboard')
}

但我得到的是未定义的

【问题讨论】:

  • 你必须包含/引用插件 vue-cookie 吗?还要检查你是否有 $cookie 对象,然后获取。
  • 我已经包含 vue-cookie
  • 如何检查 $cookie 对象,如果它不存在,那么如何包含它
  • 你告诉你的 vue 使用插件 VueCookie 吗?像 Vue.use(VueCookie)。你可以检查使用 if(this.$cookie){ 'do some cookie things here' }else{ 'no cookie here'}
  • 我用过 vue.use(VueCookie)

标签: javascript authentication cookies vue.js token


【解决方案1】:

我只是花了很多时间来处理这个问题。在条目中:

import VueCookies from 'vue-cookies'
Vue.use(VueCookies)

在 Vue 组件内部:

  console.log(this.$cookies);
  this.$cookies.set('test1', 'content');
  console.log(this.$cookies.keys());
  console.log(this.$cookies.get('test1'));

$cookies 对象定义了所有正确的函数,但keys() 返回一个空数组,get('test1') 返回null

事实证明,谷歌浏览器不支持本地文件的 cookie。尝试使用其他浏览器。

曾经有一个命令行标志允许本地文件使用 cookie,但它是seems to be have been removed

【讨论】:

    猜你喜欢
    • 2016-01-27
    • 2018-08-17
    • 1970-01-01
    • 2020-03-13
    • 2021-05-07
    相关资源
    最近更新 更多