我很难理解 Veux 4 的createStore()
工作原理。
在/store/index.js
我有(除其他几件事之外):
export function createVuexStore(){
return createStore({
modules: {
userStore,
productStore
}
})
}
export function provideStore(store) {
provide('vuex-store', store)
}
在client-entry.js
我通过商店时makeApp()
喜欢这样:
import * as vuexStore from './store/index.js';
import makeApp from './main.js'
const _vuexStore = vuexStore.createVuexStore();
const {app, router} = makeApp({
vuexStore: _vuexStore,
});
main.js
默认方法是这样做的:
export default function(args) {
const rootComponent = {
render: () => h(App),
components: { App },
setup() {
vuexStore.provideStore(args.vuexStore)
}
}
const app = (isSSR ? createSSRApp : createApp)(rootComponent);
app.use(args.vuexStore);
因此,没有store
从任何地方导出的内容,这意味着我无法导入像我这样store
的另一个.js
文件vue-router
并访问getters
ordispatch
操作。
import {store} '../store/index.js' // not possible
为了完成这项工作,我在文件中执行了以下操作vue-router.js
,但我不明白它为什么有效:
import * as vuexStore from '../store/index.js'
const $store = vuexStore.createVuexStore();
async function testMe(to, from, next) {
$store.getters('getUser'); // returns info correctly
$store.dispatch('logout'); // this works fine
}
Veux 的createStore()
方法是每次都创建一个新的商店,还是对在 中创建的同一个商店的引用client-entry.js
?看起来是后者,这是否意味着应用程序无论运行多少次都只有一个商店createStore()
?那么,为什么运行createStore()
不会覆盖现有存储并使用空白值对其进行初始化?