Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically register a Vuex plugin?

How can I register a plugin dynamically to vuex. The documentation says I can dynamically register modules etc. but not how to do it for a plugin.

Is including the plugin at store creation the only method to add plugins? I was hoping for a store.use(plugin) or vuex.use(plugin)

like image 902
mesqueeb Avatar asked Jun 21 '18 02:06

mesqueeb


People also ask

Is pinia better than Vuex?

Pinia has a Simpler API than Vuex Pinia's API is simpler and more intuitive than Vuex. Getting started with state management is much easier even for a junior developer as a lot of boilerplate code that needed to be written between every state change in Vuex has now been removed in Pinia.

What is Namespaced in Vuex?

Namespaced getters and actions will receive localized getters , dispatch and commit . In other words, you can use the module assets without writing prefix in the same module. Toggling between namespaced or not does not affect the code inside the module.

Is Vuex a plugin?

A Vuex plugin is simply a function that receives the store as the only argument: const myPlugin = (store) => { // called when the store is initialized store. subscribe((mutation, state) => { // called after every mutation. // The mutation comes in the format of `{ type, payload }`. }) }

Can you have multiple Vuex stores?

Vuex gives you the flexibility to manage multiple store modules based on how you'd like to structure your project.


1 Answers

A Vuex plugin is simply a function that receives the store as the only argument, and is invoked in the Store instance during construction.

To apply a plugin after a Store has been constructed, you just need to invoke the plugin function and pass the Store instance to that function:

import Vuex from 'vuex'
import Plugin from 'plugin'

const store = new Vuex.Store({ ... })

// Later on
Plugin(store)

Keep in mind that some plugins simply may not work correctly with already-constructed Store instances. Your milage may vary.

like image 186
Decade Moon Avatar answered Nov 27 '22 19:11

Decade Moon