【发布时间】:2018-03-31 21:48:03
【问题描述】:
我使用来自here 和here 的hackernews 方法设置vuex 和SSR,并且一切正常。
app.js:
// Expose a factory function that creates a fresh set of store, router,
// app instances on each call (which is called for each SSR request)
export function createApp () {
// create store and router instances
const store = createStore()
const router = createRouter()
// sync the router with the vuex store.
// this registers `store.state.route`
sync(store, router)
// create the app instance.
// here we inject the router, store and ssr context to all child components,
// making them available everywhere as `this.$router` and `this.$store`.
const app = new Vue({
router,
store,
render: h => h(App)
})
// expose the app, the router and the store.
// note we are not mounting the app here, since bootstrapping will be
// different depending on whether we are in a browser or on the server.
return { app, router, store }
}
存储/index.js:
Vue.use(Vuex)
export function createStore () {
return new Vuex.Store({
state: {
activeType: null,
itemsPerPage: 20,
items: {/* [id: number]: Item */},
users: {/* [id: string]: User */},
lists: {
top: [/* number */],
new: [],
show: [],
ask: [],
job: []
}
},
actions,
mutations,
getters
})
}
现在我正在尝试弄清楚如何将 store 导入 lib.js 文件,以便我可以进行一些提交,类似于他们正在尝试做的here。
在设置 SSR 之前,我只是导出了一个新的 VuexStore,以便可以将它导入到任何地方。但是对于 SSR,您需要使用工厂函数,以便为每个不同的会话创建一个新的商店实例(因此它们不会被其他商店污染)。它工作正常,但现在我无法将商店导入外部模块,因为它会创建一个新实例。
在使用 SSR 时如何将 store 导入到模块中?
【问题讨论】:
标签: vue.js vuejs2 vuex server-side-rendering