How is vuex used in vue stand-alone plug-ins? How does the vuex in the plug-in merge with the global vuex?

The problem with

is that I have a vue plug-in (which uses vue install exports and can use plug-ins introduced by vue.use ()), and I want to use vuex to communicate between components in this plug-in.
currently there are the following problems:

what I know is that when vuex is used, it is imported as a plug-in before the object is created by vue and injected into the vue object when vue is instantiated. Now my plug-in,
is just a collection of vue components, not a complete vue instance, so when exporting, you can"t rely on the vue object instantiated in the global main.js
, and you can"t use the vuex, of the injected object. How do I use vuex into components in this scenario?
Feb.28,2021

well, I have to ask and answer myself again. In the middle of the night, there are still a lot of back-end interfaces left undone. I looked at the vuex source code and didn't find the answer I wanted. Finally came up with a crooked method. (enough nonsense)

solution:

when solving this problem, you should never think like this: "the vuex in the plug-in is relatively independent of the vuex in the project that references the plug-in." what do you mean? That means you can't completely encapsulate the store in your plug-in, just register the plug-in when referencing the plug-in, and then vuex,store will happy alone in its own small scope. In fact, this is what I originally intended to look like, but I couldn't find a solution. Ask the god for advice! )

based on the above wrong ideas, half a day is gone. And then my solution was,

use vuex's module.

Yes, at the beginning of vuex design, in order to prevent all the state (state) from being put together, the state was too large to be maintained, so the state of the module using module, was well managed separately.

here, I encapsulate all my state management into a module, and export this module by the way when exporting the plug-in.

example

//moduleStore.js
const state={
    editorContent:'editorContent',
};
const mutations={};
const actions={};
const getters={
    getEditorContent(state){
        return state.editorContent;
    }
};

const editorStore={
    state,
    mutations,
    actions,
    getters
}

export default editorStore;
//plugin index.js

import module from './moduleStore.js'

const plugin={
    install:(Vue,options)=>{
    }
}

export const ms=module;    //
export const pl=plugin;

well, in that case, it's basically done. What should I do when using the plug-in?

first of all,

// main.js
import {pl} from 'plugin'; //
Vue.use(pl)  //

here, the import of the plug-in is registered, and then, in the global Vuex store,

//index.js
import {ms} from 'plugin'   // vuex store


// ms  modules
const modules={
    ms,
}

so far, the plug-in is fully integrated with the project


I have the following questions:
1, how to use vuex inside the plug-in
2, how to export the plug-in, through module.exports. Can
be exported in a named way instead of what you call, export const ms=module; / / here, and cannot be exported by default
my requirement is to import plug-ins in the form of npm packages import

look for a reply and write. -,

Menu