I have multiple VUE components in my Laravel project and many of them have the same method declared in them.
Is there a best practice for how to handle this? Or more importantly, is it possible to have these methods declared in one place and referenced by each component.
Toy Example
Apple Component
// ...
data() {
return {
page_name = 'apple'
}
},
methods: {
sayPage(page_name) {
alert(`you are on the ${page_name} page`);
}
},
mounted: {
this.sayPage(this.page_name)
}
// ...
Banana Component
// ...
data() {
return {
page_name = 'banana'
}
},
methods: {
sayPage(page_name) {
alert(`you are on the ${page_name} page`);
}
},
mounted: {
this.sayPage(this.page_name)
}
// ...
Ideally, I would like to have something like;
Shared Methods
// some file where methods can be declared once
methods: {
sayPage(page_name) {
alert(`you are on the ${page_name} page`);
}
},
Apple Component
// ...
data() {
return {
page_name = 'apple'
}
},
mounted: {
SHARED_METHODS.sayPage(this.page_name)
}
// ...
Banana Component
// ...
data() {
return {
page_name = 'banana'
}
},
mounted: {
SHARED_METHODS.sayPage(this.page_name)
}
// ...