I'm creating a small Vue Plugin that allows a user to add a "page notification" from within any ponent. I've successfully implemented something like:
this.$notifications.add("a message");
And it works! But I've had to register the mutations and actions required for my plugin to work as part of the file that sets up the rest of the store for my app:
export default new Vuex.Store({...})
Is there a way to add actions and mutations to my store from within my plugin? It currently looks like this:
import vuex from './../store';
const MyPlugin = {
install(Vue, options) {
// 4. add an instance method
Vue.prototype.$notifications =
{
notificationTypes: {
0: "warning",
1: "info"
},
add: function (options) {
let id = "page-notification-" + (vuex.state.pageNotificationsCreated + 1);
let message = options.message || options || "no message";
let notificationType = this.notificationTypes[0];
if(options.notificationType && Number.isInteger(options.notificationType)){
// Map int to string type
notificationType = this.notificationTypes[options.notificationType] || notificationType;
}else{
// Or use string we were provided ;)
notificationType = options.notificationType;
}
let notification = { id: id, message: message, notificationType: notificationType };
vuex.dispatch('addNotification', notification);
}
}
}
};
export default MyPlugin;
Any and all help appreciated!
I'm creating a small Vue Plugin that allows a user to add a "page notification" from within any ponent. I've successfully implemented something like:
this.$notifications.add("a message");
And it works! But I've had to register the mutations and actions required for my plugin to work as part of the file that sets up the rest of the store for my app:
export default new Vuex.Store({...})
Is there a way to add actions and mutations to my store from within my plugin? It currently looks like this:
import vuex from './../store';
const MyPlugin = {
install(Vue, options) {
// 4. add an instance method
Vue.prototype.$notifications =
{
notificationTypes: {
0: "warning",
1: "info"
},
add: function (options) {
let id = "page-notification-" + (vuex.state.pageNotificationsCreated + 1);
let message = options.message || options || "no message";
let notificationType = this.notificationTypes[0];
if(options.notificationType && Number.isInteger(options.notificationType)){
// Map int to string type
notificationType = this.notificationTypes[options.notificationType] || notificationType;
}else{
// Or use string we were provided ;)
notificationType = options.notificationType;
}
let notification = { id: id, message: message, notificationType: notificationType };
vuex.dispatch('addNotification', notification);
}
}
}
};
export default MyPlugin;
Any and all help appreciated!
Share Improve this question asked Aug 11, 2017 at 21:41 Daniel BrownDaniel Brown 3,0625 gold badges32 silver badges45 bronze badges1 Answer
Reset to default 21Import vuex store into your plugin; make it hard to reuse. You should put dependences in plugin options.
const MyPlugin = {
install(vue, { store }){ // now your plugin depend on store
if (!store) {
throw new Error("Please provide vuex store.");
}
// register your own vuex module
store.registerModule({states, mutations, actions });
// hook vue
}
}
Now your plugin total decouple with vuex; easy to reuse in other application.