vuejs2 - nuxtjs 中的 vuex 模块模式

标签 vuejs2 vuex nuxt.js

我试图在 nuxtjs 的 vuex 存储中使用模块模式实现一个待办事项列表,但收到错误 this.$store.todo 未定义,无法找到与 nuxt 相关的太多信息

有人可以帮忙吗

存储index.js

export const state = () => ({

})

export const mutations = {

}

存储 todo.js

export const state = () => ({
    todos: [],
})

export const mutations = {
  mutations ...
}

export const actions = {
  actions ...
} 

export const getters = {
  getters ...
}

index.vue页面

<template>
  <div>

    <h2>Todos:</h2>
    <p> Count: {{ doneTodosCount }} </p>

      <ul v-if="todos.length > 0">
        <li v-for="(todo, i) in todos" :key="i">
          ...
      </li>
    </ul>

    <p v-else>Done!</p>

    <div class="add-todo">
        <input type="text" v-model="newTodoText">
        <button @click="add">Add todo</button>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions, mapGetters } from 'vuex'

export default {
  name: 'app',

  data () {
    return {
      newTodoText: ""
    }
  },

  created () {
      this.$store.todo.dispatch('loadData')
  },

  computed: {
      ...mapState(['todos', ]),
      ...mapGetters([ 'doneTodosCount', 'doneTodos'])
  },

  methods: {
    toggle (todo) {
      this.$store.todo.dispatch('toggleTodo', todo)
    },
  }
}
</script>

根据我读到的内容,我认为这应该有效,但事实并非如此

如果我不使用模块模式并且只有一个 index.js 设置,我应该添加它一切正常

非常感谢

最佳答案

您需要以不同的方式调用它

this.$store.dispatch('todo/toggleTodo', todo)

最好也用 fetch 来调用它方法,未创建

关于vuejs2 - nuxtjs 中的 vuex 模块模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50856888/

相关文章:

Vue.js 在 ajax 请求期间禁用组件

javascript - VueJs中如何通过路由名称获取特定路由数据?

typescript - 如何在没有类样式或装饰器语法的情况下获取 mapGetters、mapActions Vuex 和 typescript 的智能感知

javascript - 清空 Vuex 对象时,如何避免 Vue 项目出现未定义错误?

vuejs2 - VUE CLI-3 项目不适用于 IE-11

javascript - 使用 Vuex,当项目是对象数组的一部分时,如何从数组中删除它们?

javascript - Nuxt.js 使用 nuxtServerInit 和 Vuex 在服务器端加载数据

laravel-5 - 类型错误 : Cannot set property 'posts' of undefined - Vuejs

javascript - VueJS 插槽 v 文本

javascript - 某些组件样式未包含在构建中