无法在 vue 中使用插件 [英] Can't use plugins in vue

查看:70
本文介绍了无法在 vue 中使用插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在浏览器中管理cookies并使用这个外部插件vue-cookies.但是如果我在一些 vue 文件中调用它的方法 this.$cookies.get(...) 我在浏览器中得到一个错误:TypeError: 无法设置未定义的属性$cookies"

I want to manage cookies in the browser and use this external plugin vue-cookies. But if I call its method in some vue file as this.$cookies.get(...) I get an error in the browser: TypeError: Cannot set property '$cookies' of undefined

这是我的代码.主.js

Here is my code. Main.js

    import App from './App.vue'
    import router from './router'
    import VueCookies from 'vue-cookies';
    import { createApp } from 'vue'
    const app = createApp(VueCookies)
    app.use(VueCookies);
    createApp(App).use(router).mount('#app')

App.vue(或任何其他 .vue 文件)

App.vue (or any other .vue file)

beforeMount() {

    var cookie = "";
    cookie = this.$cookies.get("testname");
    console.log(cookie);
}

推荐答案

从 v1.7.4 开始,vue-cookies 仅适用于 Vue 2.

As of v1.7.4, vue-cookies only works with Vue 2.

Vue 3 需要 vue3-cookies 代替:

Vue 3 requires vue3-cookies instead:

import Vue from 'vue'
import VueCookies from 'vue3-cookies'

createApp(App)
  .use(VueCookies)
  .mount('#app')

这篇关于无法在 vue 中使用插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆