vue.js - 如何在 vuex nuxt 中获取嵌套 getter

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

我有像这样的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);
            },
          },
        },
      },
    },
  },
});

我想在我已经测试过的计算属性中使用 setter/getter

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

屁股出现错误

[vuex] unknown getter: nav/message/getCount

这里缺少什么

我还想为每个模块创建单独的文件夹,例如我的导航有 3 个模块消息、请求和通知

我确实尝试过,但 nuxt 破坏了我的代码

最佳答案

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

在你的 store/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>

只要有可能,就把你的代码分成更小的部分,每件事一个部分。这使您可以更轻松地更新并使一切井井有条。

希望这有帮助。

关于vue.js - 如何在 vuex nuxt 中获取嵌套 getter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59287712/

相关文章:

html - 为许多客户端项目配置 nginx

javascript - 如何更改b-table Vue JS中列数据的颜色

sql-server - 当结果大小太大时,使用 knex 的 SQL Server 查询会使 Nuxt 应用程序崩溃

nuxt.js - vue-meta 拒绝在 nuxt 应用程序上呈现服务器端

vue.js - 如何在 nuxt.js 应用程序中使用 BugSnag?

css - 无论如何要去除阴影,或强制 vuetify 菜单与屏幕左侧完全对齐?

Laravel Inertia JS Flash 消息仅显示一次

javascript - 如何使 vuex getter 响应式

javascript - 检查多个选项并在 vuex 中进行过滤

javascript - VueJS 不会从组件重新渲染列表