简体   繁体   English

Vue.js如何使用NPM插件

[英]Vue.js how to use npm plugins

I have tried multiple different plugins with my Vue.js cli webpack projects and I am getting the same problem with all of them, namely that once I have included a plugin, I am unable to use it. 我已经在我的Vue.js cli Webpack项目中尝试了多个不同的插件,但所有这些插件我都遇到了相同的问题,即一旦包含了插件,便无法使用它。

As an example. 举个例子。 I have tried to use the plugin at this link. 我试图在此链接上使用插件。 https://www.npmjs.com/package/vue-sessionstorage https://www.npmjs.com/package/vue-sessionstorage

Here is my main.js file 这是我的main.js文件

import Vue from 'vue'
import router from './router'
import VueMaterial from 'vue-material'
import { store } from './store'
import 'vue-material/dist/vue-material.css'
import 'vue-style-loader'
import VueSessionStorage from 'vue-sessionstorage'

import App from './App'

Vue.use(VueMaterial, VueSessionStorage);


Vue.config.productionTip = false;


/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    store,
    template: '<App/>',
    components: { App },

});

I have run npm install --save vue-sessionstorage 我已经运行npm install --save vue-sessionstorage

As you can see, I have also imported VueSessionStorage 如您所见,我还导入了VueSessionStorage

However, when I try to use it like this 但是,当我尝试像这样使用时

methods: {
            login: function () {
                console.log("Login clicked");
                this.$session.set('username','simon')
                console.log(this.$session.get('username'));
            },
            register: function () {
                console.log(this.$session.get('username'));

            }
        }

I get the error 我得到错误

vue.esm.js?65d7:563 TypeError: Cannot read property 'set' of undefined
    at VueComponent.login (Login.vue?2ddf:35)
    at boundFn (vue.esm.js?65d7:179)
    at Proxy.invoker (vue.esm.js?65d7:1821)
    at Proxy.Vue.$emit (vue.esm.js?65d7:2331)
    at click (mdButton.vue?3bf8:38)
    at HTMLButtonElement.invoker (vue.esm.js?65d7:1821)
handleError @ vue.esm.js?65d7:563
Vue.$emit @ vue.esm.js?65d7:2333
click @ mdButton.vue?3bf8:38
invoker @ vue.esm.js?65d7:1821

Vue.use does not permit multiple plugins to be initialized at once like that. Vue.use不允许像这样一次初始化多个插件。 Initialize them as two separate calls: 将它们初始化为两个单独的调用:

Vue.use(VueMaterial);
Vue.use(VueSessionStorage);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM