代码之家  ›  专栏  ›  技术社区  ›  niclas_4

导入Vuex插件而不定义存储

  •  0
  • niclas_4  · 技术社区  · 6 年前

    我的一个商店模块是这样的

    export const state = () => ({
      posts: []
    })
    
    export const getters = {}
    
    export const actions = {}
    
    export const mutations = {
      fetchedPosts(state, posts) {
        state.posts = posts
        console.log("fetched")
      },
    
      pushPost(state, post) {
        state.posts.push(post)
        console.log("pushed")
      }
    }
    

    我想使用 vuex-persistedstate 插件,但遵循他们的文档和Vuex插件文档 https://vuex.vuejs.org/guide/plugins.html 我没有找到一个方法来使用我的商店里的插件,提前感谢。

    1 回复  |  直到 6 年前
        1
  •  4
  •   Alex Freshmann    6 年前

    你只需要在你的演讲中提到一次 store/index.js :

    import Vue from 'vue'
    import Vuex from 'vuex'
    Vue.use(Vuex)
    import createPersistedState from 'vuex-persistedstate'
    import posts from '~/store/posts.store'
    
    ....
    
    export default new Vuex.Store({
      state:     { ... },
      mutations: { ... },
      actions: { ... }, 
      modules: { 
        posts,
        ... 
      },
      plugins: [createPersistedState()]
    })
    

    在帖子模块中:

    const state = () => ({ ... })
    const getters = { ... }
    const mutations = { ... }
    const actions = { ... }
    export default { namespaced: true, state, getters, actions, mutations }    
    

    这对我很有用。