简体   繁体   English

在vue.js中使用Vuex执行CRUD操作时出错

[英]Error performing a CRUD operation using Vuex in vuejs

I have been trying to add a new note but whenever I try to add it the console returns 我一直在尝试添加新笔记,但是每当我尝试添加笔记时,控制台都会返回

TypeError: _vm.addNote is not a function TypeError:_vm.addNote不是函数

and

Property or method "addNote" is not defined on the instance but referenced during render. 属性或方法“ addNote”未在实例上定义,但在渲染期间被引用。 Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. 通过初始化属性,确保在data选项中或对于基于类的组件,此属性都是反应性的。

Here is my vuex code: 这是我的vuex代码:

<pre>
export default new Vuex.Store({
  state: {
    newNote:{},
    notes: [{
      poster: 'static/assets/logo.png',
      id: 1,
      title: 'sunt aut facere repellat provident occaecati', 
      body: 'this is the body of the post'
    }
  ],
  },
  mutations: {
    ADD_NOTE:(state) => {
      state.notes.push({
        title:this.newNote.title,
        body:this.newNote.body,
      })
      this.newNote={}
   },
  },
  actions: {
    addLink : function (store) {
      var commit = store.commit
      commit('ADD_NOTE')
    },
    }
})
</pre> 

and this is my component that I'm trying to add a new note: whenever I click add it consoles addNote is not a function 这是我要添加新注释的组件:每当我单击添加它时,控制台addNote都不起作用

 1. List item


    <pre>
<form>
      <input id="text" type="text" v-modal="newNote.title">//this is where i want to add the title of the note

//this is where i want to add the body of the note`enter code here`  <textarea id="textarea1" class="materialize-textarea" v-modal="newNote.body"></textarea>
            <button type="submit" @click.prevent="addNote" class="btn blue">Add</button>
</form>

    <script>
    import { mapState , mapMutations, mapActions } from 'vuex'
    export default {
      name: "appnav",
      data() {
          return {
     newNote:{},
        computed: {
            ...mapState([
                'notes',
                'newNote'
            ]),
            ...mapActions([
                'addLink'
            ])
        },

      methods: {
          ...mapMutations([
             'ADD_NOTE' 
          ]),
          addNote () {
              this.ADD_NOTE(this.newNote)

          },
    } ,
    }
    }
    }
    </script>

Your issue is in the way you are invoking the vuex actions/mutations. 您的问题在于调用vuex操作/变异的方式。

const methods = {
   addNote () {
        this.ADD_NOTE(this.newNote) // technically this will work but is not recommended 
        this.$store.dispatch('addNote') // ERROR
      }
};

Note the difference between these APIs: 请注意这些API之间的区别:

$store.dispatch.VUEX_ACTION - Vue will search in your stores' Actions handlers, not mutations. $ store.dispatch.VUEX_ACTION -Vue将在商店的Actions处理程序中搜索, 而不是在突变中搜索。

$store.commit.VUEX_MUTATION - Vue will search in your stores' Mutations handlers, not actions. $ store.commit.VUEX_MUTATION -Vue将搜索商店的Mutations处理程序, 而不是操作。

So given that, your error is due to the fact that you don't have an addNote function defined as an action in your store; 因此,您的错误是由于您的商店中没有将addNote函数定义为操作而导致的; addLink is the only action you have defined. addLink是您定义的唯一操作。

On a another note - you are attempting to perform the same operation twice in a row by first calling this.addNote , followed by this.$store.dispatch . 另一个要注意的是,您试图连续执行两次相同的操作,方法是先调用this.addNote ,然后调用this.$store.dispatch If you want to "futureproof" your app, do not mapMutations in your component, only mapActions . 如果您想为“面向未来的”你的应用程序,不要mapMutations在组件中,只有mapActions Subsequently, your vuex actions will be the only function that directly invoke your mutations. 随后,您的vuex动作将是直接调用您的变异的唯一功能。

The reason why there is this intermediate step is described in the documentation and is as follows: 文档中介绍了执行此中间步骤的原因,如下所示:

Actions are triggered with the store.dispatch method: 使用store.dispatch方法触发操作:

store.dispatch('increment') store.dispatch( '增量')

This may look silly at first sight: if we want to increment the count, why don't we just call store.commit('increment') directly? 乍一看,这可能看起来很愚蠢:如果我们想增加计数,为什么不直接调用store.commit('increment')? Remember that mutations have to be synchronous? 还记得突变必须是同步的吗? Actions don't. 动作不行。 We can perform asynchronous operations inside an action. 我们可以在一个动作中执行异步操作。

So to summarize, mutations must by synchronous. 综上所述,突变必须是同步的。 Imagine that instead of simply 'adding a note' to your local memory you are performing a post request to some backend or external database, this would certainly not be a synchronous routine and thus you have to rewrite your vuex store (and components) to dispatch actions instead of committing mutations directly in your components. 想象一下,您不是向本地后端或外部数据库执行发布请求,而不仅仅是在本地内存中添加注释,这肯定不是同步例程,因此您必须重写vuex存储(和组件)才能分派操作,而不是直接在组件中进行更改。

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

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