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?
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.
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.
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.
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)
}
}
})
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With