Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue - best way to reuse methods

In vue 2 - what is correct practise for reusing vue methods?

Instead of writing them in each component, what is the best way to make them global?

like image 775
Qar Avatar asked Mar 10 '17 16:03

Qar


People also ask

How do you reuse Vue components?

Component Registration Each component you create in your Vue app should be registered so Vue knows where to locate its implementation when it is encountered in a template. To register your component in another component, you should add it to the components property in the Vue object.

What is Vue mixin?

Mixins are a flexible way to distribute reusable functionalities for Vue components. A mixin object can contain any component options. When a component uses a mixin, all options in the mixin will be “mixed” into the component's own options.

Is Vue 3 a component?

What are components in Vue 3? Components are reusable pieces of UI code that build functional web applications. They can either be used as building blocks of complex applications, or as reusable pieces to prevent rewriting similar pieces of code repeatedly.


2 Answers

Mixin

You can create a mixin for it

Mixins are a flexible way to distribute reusable functionalities for Vue components. A mixin object can contain any component options. When a component uses a mixin, all options in the mixin will be “mixed” into the component’s own options.

Example:

// define a mixin object
var myMixin = {
  created: function () {
    this.hello()
  },
  methods: {
    hello: function () {
      console.log('hello from mixin!')
    }
  }
}
// define a component that uses this mixin
var Component = Vue.extend({
  mixins: [myMixin]
})
var component = new Component() // -> "hello from mixin!"

If you dont want to mix it manually in all components, you can use global mixin

// inject a handler for `myOption` custom option
Vue.mixin({
  created: function () {
    var myOption = this.$options.myOption
    if (myOption) {
      console.log(myOption)
    }
  }
})

Plugin

Another way to create a method available globally is to create a plugin.

MyPlugin.install = function (Vue, options) {
  //  add global method or property
  Vue.myGlobalMethod = function () {
    // something logic ...
  }
  //  inject some component options
  Vue.mixin({
    created: function () {
      // something logic ...
    }
    ...
  })
  //  add an instance method
  Vue.prototype.$myMethod = function (options) {
    // something logic ...
  }
}

Use plugins by calling the Vue.use() global method:

Vue.use(MyPlugin)

Now these methods will be available everywhere. You can see an example of this here.

like image 178
Saurabh Avatar answered Oct 06 '22 20:10

Saurabh


There is no really good way of code reusing mechanisms in Vue2, they are just workarounds and sometimes if not carefully designed they add more headaches than they provide. But fortunately, Vue3 offered exactly what was missing from Vue for a long time, new Composition API (true composition instead of merging logic (mixins) or wrapping logic (slots or scoped-slots). Even it is part of Vue 3, you can still use it as a separate module in your Vue2 app. Composition API gives the same joyful development experience as once Hooks brought into React community.

like image 41
Humoyun Ahmad Avatar answered Oct 06 '22 20:10

Humoyun Ahmad