Answer the question
In order to leave comments, you need to log in
How to set global methods in Vue?
Tell me how to set global methods in vue.js
So that I could then use them in any module
Both for displaying information
And in module methods<p>{{ globalMetodName(param1, param2) }}<p>
modeleMetod: function(){
this.globalMetodName(param1, param2);
}
Answer the question
In order to leave comments, you need to log in
You can also use the mixin mechanism: https://ru.vuejs.org/v2/guide/mixins.html
Create a mixin object:
// определяем объект примеси
var myMixin = {
created: function () {
this.hello()
},
methods: {
hello: function () {
console.log('привет из примеси!')
}
}
}
Vue.mixin(myMixin)
Make a plugin. Example: https://jsfiddle.net/wostex/63t082p2/45/
<div id="app">
<p>{{ helloText }}</p>
</div>
const mylib = {
hello : (text1, text2) => {
return `Hello! ${text1} ${text2}`
},
install: function(Vue){
Object.defineProperty(Vue.prototype, 'mylib', {
get () { return mylib }
})
}
};
Vue.use(mylib);
new Vue({
el: '#app',
data: {
helloText: mylib.hello('one', 'two')
}
});
What's wrong with mapActions (at the bottom of the page) and mapGetters (at the bottom of the page) ? Unless, of course, you are using Vuex in the project.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question