在 Vuex Store 模块中使用 Vue.js 插件

Fre*_*rik 9 vue.js vuex

我正在尝试在 Vuex Store 模块中使用 Vue.js 插件。

在一个组件,我可以这样称呼它:this.$plugin()。但是,在一个模块中this没有设置。我想Vue.$plugin()会的工作,因为我初始化插件Vue.use(plugin)Vue公司是一个全局变量,但事实并非如此。

如何从模块中引用插件?

Fre*_*rik 2

Bert在此处提供的示例中回答了这个问题: https ://codesandbox.io/s/jp4xmzl0xy

    import Vue from 'vue'
    import App from './App'
    import Notifications from 'vue-notification'
    import Vuex from "vuex"
    Vue.use(Notifications)
    Vue.use(Vuex)
    
    let notifier = new Vue()
    
    
    const store = new Vuex.Store({
      state:{},
      actions:{
        notify(context, payload){
          notifier.$notify(payload)
        }
      }
    })
    
    
    /* eslint-disable no-new */
    new Vue({
      el: '#app',
      store,
      template: '<App/>',
      components: { App }
    })
Run Code Online (Sandbox Code Playgroud)