【问题标题】:Importing store into a lib.js while using vuex with SSR将 vuex 与 SSR 一起使用时将商店导入 lib.js
【发布时间】:2018-03-31 21:48:03
【问题描述】:

我使用来自herehere 的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


    【解决方案1】:

    您在@/store/index.js 的文件正在导出工厂函数。它制作 Vuex 商店。您需要导出在 @/app.js 中创建的商店实例

    // A singleton Vuex store
    export var store
    
    // 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 router = createRouter()
    
      if (!store) store = createStore()
    
      // 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 }
    }
    

    【讨论】:

      【解决方案2】:

      我尝试了here 的解决方案,但它对我不起作用。所以我尝试了下面详述的我自己的解决方案,它似乎有效。 在 store/index.js 中添加 clientStore 的导出

      Vue.use(Vuex)
      
      const storeOptions = {
          state: {
              activeType: null,
              itemsPerPage: 20,
              items: {
                  /* [id: number]: Item */
              },
              users: {
                  /* [id: string]: User */
              },
              lists: {
                  top: [
                      /* number */
                  ],
                  new: [],
                  show: [],
                  ask: [],
                  job: []
              }
          },
          actions,
          mutations,
          getters
      }
      
      // Factory function for SSR
      export function createStore() {
          return new Vuex.Stores(storeOptions)
      }
      
      // Client store for client side
      export const clientStore = new Vuex.Store(storeOptions)
      

      然后在你的 app.js 中你可以导入它并像这样使用它

      import { clientStore, createStore } from '../store/index'
      .
      .
      .
      // 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
          let router
          let store
          if (process.env.VUE_ENV === 'server') {
              router = createRouter()
              store = createStore()
          } else {
              router = createRouter()
              store = clientStore
          }
      
          // 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 }
        }
      

      【讨论】:

      • 这不会创建 2 个不同的商店实例吗?
      • 对于 SSR,您希望每个用户有一个不同的实例,而在客户端上您只需要一个。实例不同,但您使用 entry-client.js 中的服务器存储数据来启动客户端存储。然后,您在客户端中只有一个实例,您可以将其导入到您想要的任何文件中。
      猜你喜欢
      • 2022-12-25
      • 2021-03-18
      • 2019-11-03
      • 2022-09-23
      • 2022-01-03
      • 2020-02-10
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      相关资源
      最近更新 更多