4

Is there a proper / documented way of using a plugin inside vuex module or plain js module? I am using event bus to acheive it, not sure if it is the correct / best way. Please help.

Plugin1.plugin.js:

const Plugin1 = {
  install(Vue, options) {
    Vue.mixin({
      methods: {
        plugin1method(key, placeholderValues = []) {
          return key;
        },
      },
    });
  },
};

export default Plugin1;

In App.vue:

Vue.use(Plugin1, { messages: this.plugin1data });

In store / plain-js module:

const vue = new Vue();
const plugin1method = vue.plugin1method;
JVM
  • 946
  • 1
  • 10
  • 23
  • Plugins are typically used within `Vue` instances / components. It might help if you explain what plugin you're trying to use as some add static methods to the `Vue` class – Phil Oct 24 '18 at 22:59
  • If you are talking about Vuex plugins, have a look here: https://vuex.vuejs.org/guide/plugins.html. But I guess you are looking for using Vue plugins within Vuex. – wwerner Oct 24 '18 at 23:51
  • It is a plugin currently used in components. I want to use the same plugin in vuex – JVM Oct 25 '18 at 17:41
  • downvoter, please mention whats wrong in this question? I am working on a complex vue.js project and hold enough knowledge to ask legit questions. – JVM Oct 25 '18 at 22:47
  • I think @Daniel put it best in his answer below... _"I can't tell you which way you should use it because I **can't see how your function is defined in your plugin**"_. Please add some more details to your question – Phil Oct 26 '18 at 02:47

1 Answers1

7

you can access your Vue instance using this._vm;
and the Vue global using import Vue from 'vue'; and then Vue;

I'm guessing you defined an instance method, so it would be the former (this._vm.plugin1method())


update

I can't tell you which way you should use it because it I can't see how your function is defined in your plugin.

However, here is an example that should illustrate the difference between instance and global

const myPlugin = {
  install: function(Vue, options) {
    // 1. add global method or property
    Vue.myGlobalMethod = function() {
      // something logic ...
      console.log("run myGlobalMethod");
    };
    Vue.mixin({
      methods: {
        plugin1method(key, placeholderValues = []) {
          console.log("run mixin method");
          return key;
        }
      }
    });
    // 4. add an instance method
    Vue.prototype.$myMethod = function(methodOptions) {
      console.log("run MyMethod");
      // something logic ...
    };
  }
};

Vue.use(Vuex);
Vue.use(myPlugin);

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment(state) {
      this._vm.$myMethod();
      Vue.myGlobalMethod();
      this._vm.$options.methods.plugin1method();  // <-- plugin mixin custom method
      state.count++;
    }
  }
});

when you commit the increment ie: this.$store.commit('increment') both methods will execute

Daniel
  • 34,125
  • 17
  • 102
  • 150
  • Actually, my plugin is a global plugin (https://vuejs.org/v2/guide/plugins.html). It is loaded using 'Vue.use'. Based on this info, what should be the correct way? – JVM Oct 25 '18 at 22:13
  • 1
    I've updated the answer. Hopefully this clears it up. https://codesandbox.io/s/wooz0lm9l8 – Daniel Oct 26 '18 at 02:34
  • Added the plugin code to have more clarity in my question. One restriction is, I cannot change the plugin code. It is using a mixin there. – JVM Oct 26 '18 at 04:37
  • in that case it's an instance, I've updated answer to include that too – Daniel Oct 26 '18 at 15:30
  • @Daniel How then would you be able to access or read `this._vm.$myMethod()` in jest unit tests? It seems you 'cant read propery $myMethod of undefined'. – Kevin T. Aug 27 '19 at 16:30
  • @KevinT. you should be able to access it via `wrapper.vm` src: https://vue-test-utils.vuejs.org/guides/#getting-started – Daniel Aug 27 '19 at 19:15