javascript - Vuex Store Module 访问状态

标签 javascript vue.js vuejs2 vuex

我想知道如何从另一个文件访问模块存储/状态。 到目前为止,这是我的代码:

/store/index.js

import Vuex from 'vuex';
import categories from './modules/categories';

Vue.use(Vuex);
const store = new Vuex.Store({
  state: {
  },
  actions: {

  },
  mutations: {

  },
  getters: {

  },
  modules: {
    categories,
  },
});
export default store;

/store/modules/categories.js

const categories = {
  state: {
    categories: [
      {
        name: 'category1'
        path: 'path/to/there'
        subcategories: [
          {
            name: 'subcategory1'
            path: 'path/to/other/there'
            subsubcategory: [
              {
                name: 'subsubcategory1'
                path: 'path/to/other/there'
              }
              {
                name: 'subsubcategory1'
                path: 'path/to/other/there'
              }
            ]
          }
          {
            name: 'subcategory2'
            path: 'path/to/other/there'
          }
        ]
      }
      {
        name: 'category2'
        path: 'path/to/there'
        subcategories: [
          {
            name: 'subcategory2'
            path: 'path/to/other/there'
          }
          {
            name: 'subcategory3'
            path: 'path/to/other/there'
          }
        ]
      }
    ]
  },
  actions: {

  },
  mutations: {

  },
  getters: {

  },
}

编辑:我希望能够在这里访问模块状态/存储,例如: /home.vue

<template>
  <div>
    <Headers></Headers>
    <div class="user row">
      <p>User Page</p>
      <p> I want to be able to access modules store/state here and be able to pass getters here to filter some results from state</p>
    </div>
    <Footers></Footers>
  </div>
</template>

<script>

import 'vue-awesome/icons';
import { mapState, mapGetters } from 'vuex';
import Headers from '/Headers';
import Footers from '/Footers';

export default {
  name: 'home',
  data() {
    return {
    };
  },
  methods: {
  },
  components: {
    Headers,
    Footers,
  },
  computed: {
    ...mapGetters([
      '',
    ]),
    ...mapState([
      'categories',
    ]),
  },
};
</script>

<style lang="scss" scoped>

</style>

现在,我之前能够访问和循环浏览类别,但感谢 @Sumit-Ridhal我发现我的商店模块是错误的,所以我不得不更改它,现在我不知道如何访问它的状态。

提前致谢, 干杯

最佳答案

store.state."模块名"."状态数据

我是这样存储模块的。

这是索引存储。商店/index.js

import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import posts from '@/store/posts' // import external store mod
Vue.use(Vuex)
export default new Vuex.Store({
  modules: {
    posts
  },
  strict: true,
  plugins: [
    createPersistedState()
  ],
  state: {...... etc

帖子商店模块

  // how to access this store console.log('module state', store.state.posts.data)
    export default {
      state: {
        data: 'dfsd'
      },
      getters: {
      },
      mutations: {
      },
      actions: {
      }
    }

获取商店模块数据

从“@/store/”导入商店

store.state.posts.data

关于javascript - Vuex Store Module 访问状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43738186/

相关文章:

javascript - 搜索 Javascript 对象数组

javascript - css ul 列表多项对齐

javascript - 视觉 : Reasons to use props instead of referencing parent data?

vue.js - 是否可以使用 Vuetify/VueJS 设置默认图像而不是轮播的第一个图像?

node.js - 在 Electron 应用程序中运行时加载 Nodejs 模块

javascript - 视觉 : Components/templates as props

vue.js - Vuejs - vuex 延迟加载

javascript - 在 Ajax 请求中返回命名数组

javascript - 单击链接并重定向时如何添加事件类?

javascript - 复选框过滤结果