I'd like to group some of my Vue.js methods together in a sort of "submethod" class, but I only seem to be able to have single level methods.
For example, if I wanted to have a set of methods dealing purely with button actions:
new Vue({
el: '#app',
data: { },
methods: {
buttonHandlers: {
handler1: function() {
dosomething;
},
handler2: function() {
dosomething;
}
}
}
});
I would expect to be able to then use something like:
<button v-on:click="buttonHandlers.handler1">Click Me</button>
but nothing happens.
I have tried forcing the function to run by adding brackets:
<button v-on:click="buttonHandlers.handler1()">Click Me</button>
but I receive this console error:
Uncaught TypeError: scope.buttonHandlers.handler1 is not a function
I've setup a small https://jsfiddle.net/ozx9oc4c/ to demonstrate what I mean.
If anyone knows of a way to logically group functions under parent methods in Vue.js, rather than pages and pages of single level methods with no real structure, I'd be grateful for your knowledge.
Call one method, apparently any method of a component from any other component. Just add a $on function to the $root instance and call form any other component accessing the $root and calling $emit function.
Vue comes with the reactive method, which is used to create a reactive state in JavaScript or Vue. Basically, the reactive method is just a function that creates a proxy and wraps it around provided data objects, ultimately turning it into a proxied object.
May 11, 2020. The mounted() hook is the most commonly used lifecycle hook in Vue. Vue calls the mounted() hook when your component is added to the DOM. It is most often used to send an HTTP request to fetch data that the component will then render.
The closest I've got to doing this, is to declare the parent as a function, and return an object with a set of methods.
Example:
new Vue({
el: '#app',
data: {},
methods: {
buttonHandlers: function() {
var self = this; // so you can access the Vue instance below.
return {
handler1: function() {
dosomething;
self.doSomething();
},
handler2: function() {
dosomething;
},
},
}
}
});
And you can call the methods like this:
<button @click="buttonHandlers().handler1()">Click Me</button>
There is actually a very simple technique: define your nested methods in the created
hook:
created() {
this.on = {
test: () => {console.log(this)}
}
this.on.test();
}
NOTE: Two things, A) in this case you must use arrow function(s) and B) if this feels "hacky" to you, perhaps because of the cluttering of the created
lifecycle hook, you can always delegate to a method, let's say this.namespaceMethods()
, e.g.:
created() {
this.namespaceMethods();
// call namespaced methods
this.foo.bar();
this.foobar.baz();
// etc.
},
methods: {
this.namespaceMethods() {
this.foo = {
bar: () => {console.log("foobar")}
},
this.foobar = {
baz: () => {console.log("foobarbaz")}
}
},
// etc
}
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