【问题标题】:how to get nested getters in vuex nuxt如何在 vuex nuxt 中获取嵌套的 getter
【发布时间】:2020-04-04 20:22:19
【问题描述】:

我有这样的store/index.js

new Vuex.Store({
  modules: {
    nav: {
      namespaced: true,
      modules: {
        message: {
          namespaced: true,
          state: {
            count: 0,
            conversations: [],
          },
          getters: {
            getCount: state => {
              return state.count;
            },
          },
          mutations: {
            updateCount(state) {
              state.count++;
            },
          },
          actions: {},
        },
        requests: {
          namespaced: true,
          state: {
            friends: [],
          },
          getters: {
            getFriends: state => {
              return state.friends;
            },
          },
          mutations: {
            pushFriends(state, data) {
              state.friends.push(data);
            },
          },
          actions: {
            pushFriends(commit, data) {
              commit('pushFriends', data);
            },
          },
        },
      },
    },
  },
});

我想在我测试过的计算属性中使用 getter

computed: {
    ...mapGetters({
      count: 'nav/message/getCount',
    }),
  },

对接错误

[vuex] 未知 getter:nav/message/getCount

这里缺少什么

我也想为每个模块创建单独的文件夹,比如我的导航有 3 个模块message, requests & notifications

我确实尝试过,但 nuxt 炸毁了我的代码

【问题讨论】:

  • 试试这个:count: 'getCount',
  • count: 'nav/message/getCount' 有什么不同
  • 我认为你的代码是错误的。在您的商店中,每个文件都是一个子模型,因此您不需要创建嵌套属性。创建一个新文件,例如。 messages.js 并且您可以使用 messages/getterName。
  • @henrique 你能发布工作代码吗
  • 请修正您的标题 NASTED 错误,改为 NESTED。谢谢

标签: vue.js vuex nuxt.js vuex-modules


【解决方案1】:

我认为您的索引是错误的,正确的做法是独立分离模块,如下所示:

在您的商店/index.js 中

    export const state = () => ({
      config: {
        apiURL: 'https://meuapp.com'
      }
    })
    
    export const mutations = { }        
    export const actions = { }

    // getters
    export const getters = { 
      test: state => payload => {
        if (!payload)
          return {
            message: 'this is an messagem from index without payload test.', // you don't need pass any payload is only to show you how to do.
            result: state.config
          }
        else 
          // return value
          return {
            message: 'this is an message from index test with payload.',
            result: state.config, // here is your index state config value
            payload: payload // here is yours params that you need to manipulate inside getter
          }
      } 
    }

这是你的商店/navi.js

    export const state = () => ({
      navi: {
        options: ['aaa', 'bbb', 'ccc']
      }
    })

    export const mutations = { }
    export const actions = { }
    
    // getters
    export const getters = { 
      test: state => payload => {
        if (!payload)
          return {
            message: 'this is a messagem from nav store without payload test.', // you don't need pass any payload is only to show you how to do.
            result: state.navi
          }
        else 
          // return value
          return {
            message: 'this is an messagem from navi test with payload.',
            result: state.navi, // here is your index state config value
            payload: payload // here is yours params that you need to manipulate inside getter
          }
      } 
    }
    

然后在您的组件中,您可以将其用作计算属性:

    <template>
      <div>
        without a paylod from index<br>
        <pre v-text="indexTest()" />
    
        with a paylod from index<br>
        <pre v-text="indexTest( {name: 'name', other: 'other'})" />
    
        without a paylod from navi<br>
        <pre v-text="naviTest()" />
    
        with a paylod from navi<br>
        <pre v-text="naviTest( {name: 'name', other: 'other'})" />
    
        access getters from methods<br>
        <pre>{{ accessGetters('index') }}</pre>
        <pre v-text="accessGetters('navi')" />
        <br><br>
    
      </div>
    </template>
    
    <script>
    import {mapGetters} from 'vuex'
    export default {
      computed: {
        ...mapGetters({
          indexTest: 'test',
          naviTest: 'navi/test'
        })
      },
      methods: {
        accessGetters (test) {
          if (test && test === 'index' ) {
            console.log('test is', test) // eslint-disable-line no-console
            return this.indexTest()
          }
          else if (test && test === 'navi') {
            console.log('test is:', test) // eslint-disable-line no-console
            return this.naviTest()
          }
          else {
            return 'test is false'
          }
        }
      }
    }
    </script>

尽可能将您的代码分成更小的部分,每件事一个部分。这使您可以更轻松地更新和保持一切井井有条。

希望这会有所帮助。

【讨论】:

  • @sidheart 请修正你的标题 NASTED 错误,改成 NESTED。
猜你喜欢
  • 2019-10-19
  • 1970-01-01
  • 2021-07-15
  • 2020-08-18
  • 2018-05-22
  • 1970-01-01
  • 2019-10-07
  • 1970-01-01
  • 2020-12-21
相关资源
最近更新 更多