简体   繁体   English

Nuxt vuex state menuList:组件中未定义

[英]Nuxt vuex state menuList:undefined in component

guy.家伙。 I'm novice programmer.我是新手程序员。

I have question for nuxt use vuex get data for api.我对 nuxt 使用 vuex 获取 api 的数据有疑问。

so data call in vue value null but in dev tool vue has data.所以数据调用 vue 值 null 但在开发工具 vue 中有数据。

How to use getters in nuxt.如何在 nuxt 中使用 getter。

This image from dev tool.此图像来自开发工具。 在此处输入图像描述

In getters and state chachange: menuList has array data but in component not data.在 getter 和 state chachange 中:menuList 有数组数据,但在组件中没有数据。

This image from component此图像来自组件在此处输入图像描述

Component MenuList> computed> menuList: undefined.组件菜单列表>计算>菜单列表:未定义。

This my menu_list.vue code.这是我的 menu_list.vue 代码。

computed: {
    menuList () {
      // eslint-disable-next-line no-console
      return this.$store.getters.getMenuList
    }
  },
  beforeCreated () {
    // eslint-disable-next-line no-console
    console.log(this.$store.state)
    this.$store.dispatch('chachang/fetchMenu')
  },
  created () {
    // eslint-disable-next-line no-console
    console.log(this.$store.state)
    this.$store.dispatch('chachang/fetchMenu')
  }

This vuex code.这个 vuex 代码。 Chachang/state.js查昌/state.js

export default () => ({
  menuList: []
})

Chachang/actions.js查昌/actions.js

export default {
  async fetchMenu ({ commit }) {
    const response = await this.$axios.get('https://hlkittipan.github.io/rock-paper-scissors/menu.json')
    commit('SET_MENU', response.data)
  }
}

Chachang/mutations.js查昌/mutations.js

export default {
  SET_MENU (state, menu) {
    state.menuList = menu
  }
}

Chachang/getters.js查昌/getters.js

export default {
  getMenuList: (state) => {
    return state.menuList
  }
}

This store/index.js这个商店/index.js

import chachangActions from './chachang/actions'
import chachangStates from './chachang/state'
import chachangGetters from './chachang/getters'
import chachangMutations from './chachang/mutations'

export const state = () => ({
  ...chachangStates.state,
})

export const mutations = {
  ...chachangMutations.mutations, 
}

export const actions = {
  ...chachangActions.actions
}

export const getters = {
  ...chachangGetters.getters
}

I will advice you to use mapActions and mapGetters that are imported from vuex.我会建议你使用从 vuex 导入的 mapActions 和 mapGetters。

import { mapGetters, mapActions } from 'vuex'从 'vuex' 导入 { mapGetters, mapActions }

export default{
...
   methods:{
      ...mapActions('chachang',[ 'fetchMenu' ])
   },
   mounted(){
      this.fetchMenu() // better to use fetchData instead 
   }
   computed:{
      ...mapGetters('chachang', [ 'menuList' ])
   }

..
}

Try this.尝试这个。

  async beforeMount () {
        const data = await this.$store.dispatch('chachang/fetchMenu')
        // eslint-disable-next-line no-console
        console.log(data)
      },computed: {
        menuList () {
          // eslint-disable-next-line no-console
          return this.$store.getters['chachang/getMenuList']
        }
      },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM