[Vuejs]-Invoke plugin method in vue

0👍

Since your plugin creates a mixin, you don’t need to import the plugin into your components, and generally you wouldn’t do so with any plugin. But there are some syntax errors and a missing methods option. Try this:

const throwingError = {}
throwingError.install = function (Vue){
  Vue.mixin({
    methods: {  // You missed this, `LogEvent` is a method
      LogEvent(event, msg){  // This had a wrong argument name
        try {
          if(event){
            throw new Error(msg)
          }
        } catch (error) {
          console.error("Error Message:", error.message); // This syntax was wrong
        }
      }
    }
  });
}
Vue.use(throwingError);

/***** APP *****/
new Vue({
  el: "#app",
  mounted() {
    this.LogEvent({}, 'test'); // This was missing the method name
  }
});

Vue.config.productionTip = false;
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <button v-on:click="LogEvent($event, 'hi')">
    Click to trigger throwingError method
  </button>
</div>

Leave a comment