【问题标题】:Nuxt.js / Vuex - using Namespace modules in mapActions helper, [vuex] unknown action type: FETCH_LABELNuxt.js / Vuex - 在 mapActions 帮助器中使用命名空间模块,[vuex] 未知操作类型:FETCH_LABEL
【发布时间】:2019-01-21 03:22:52
【问题描述】:

我正在尝试使用来自 vuex 的 mapActions 助手将操作映射到组件。这是我的 labels.js vuex 模块:

export const FETCH_LABELS = 'FETCH_LABELS'
export const FETCH_LABEL = 'FETCH_LABEL'

const state = () => ({
  labels: [
    { name: 'Mord Records', slug: 'mord', image: '/images/labels/mord.jpg'},
    { name: 'Subsist Records', slug: 'subsist', image: '/images/labels/subsist.jpg'},
    { name: 'Drumcode Records', slug: 'drumcode', image: '/images/labels/drumcode.png'},
  ],
  label: {} // null
})

const mutations = {
  FETCH_LABEL: (state, { label }) => {
    state.label = label
  },
}

const actions = {
  fetchLabel({commit}, slug) {
    
    let label = state.labels.filter((slug, index) => {
      return slug == state.labels[index]
    })

    commit(FETCH_LABEL, { label })
  },
}

const getters = {
  labels: state => {
    return state.labels
  },

  label: (state, slug) => {
  }
}

export default {
  state,
  mutations,
  actions,
  getters
}

这是我想要映射 fetchLabel 操作的组件 _slug.vue 页面:

<template>
  <div class="container">

        <div class="section">
            <div class="box">
                <h1>{{ $route.params.slug }}</h1>
            </div>
         
        </div>
    </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";

export default {
  data() {
    return {
      title: this.$route.params.slug
    };
  },
  computed: {
    // Research
    // labels() {
    //   return this.$store
    // }

    ...mapGetters({
      labels: "modules/labels/labels"
    })
  },
  components: {},
  methods: {
    ...mapActions({
      fetchLabel: 'FETCH_LABEL' // map `this.add()` to `this.$store.dispatch('increment')`
    })
  },
  created() {
    console.log('created')
    this.fetchLabel(this.$route.params.slug)
  },
  head() {
    return {
      title: this.title
    }
  },
  layout: "app",
}
</script>

<style>
</style>

但是在this.fetchLabel(this.$route.params.slug)created() 生命周期钩子中,它会在控制台中引发以下错误:

[vuex] 未知动作类型:FETCH_LABEL

我错过了什么或做错了什么?请帮我解决这个问题。

【问题讨论】:

    标签: vue.js vuex nuxt.js


    【解决方案1】:

    注意在 Nuxt.js 中:

    模块:store 目录中的每个 .js 文件都被转换为命名空间模块(索引是根模块)。

    您正在使用:

    这是我的 labels.js vuex 模块:

    如上所述,使用 labels.js,因此您需要以命名空间模块的形式访问所有内容,因此您的 mapAction 助手应该是这样的:

    methods: {
      ...mapActions({
        nameOfMethod: 'namespace/actionName'
      })
    }
    

    所以你会得到这个:

    ...mapActions({
      fetchLabel: 'labels/fetchLabel'
    })
    

    当您希望将操作名称保留为方法名称时,您也可以这样做来清理它。

    ...mapActions('namespace', ['actionName']),
    ...
    

    所以你会得到这个:

    ...mapActions('labels', ['fetchLabel']),
    ...
    

    在这两种情况下,计算的 prop 都应该可以正常工作。

    【讨论】:

      【解决方案2】:

      您的操作名称是 fetchLabel 而不是 FETCH_LABEL(这是一个突变)。在mapActions改成

      methods: {
        ...mapActions({
           fetchLabel
        })
      },
      

      【讨论】:

      • 我已经尝试过这种方法。它说“未定义fetchLabel”,我做不到。
      • 可以是 fetchLabel: 'modules/labels/fetchLabel' 吗?我想我设法让它像这样工作?然而,这对于 nuxt.js 来说是常见的吗?
      • 由于 nuxt 将 store 目录中的所有 .js 文件视为命名空间 fetchLabel:'labels/fetchLabel' 将是正确的方式和规范。
      猜你喜欢
      • 2020-09-27
      • 2021-05-17
      • 2020-05-21
      • 2018-10-02
      • 2020-07-24
      • 2021-11-28
      • 2018-05-18
      • 2019-03-13
      • 2019-04-18
      相关资源
      最近更新 更多