Use Vuex given unknown action type: XXX (unknown type of operation :)

Just want to implement a simple count ++ count - function
less than six months did not use vue suddenly found with the
unknown action type: the error looked for a long time, spent a good long time, and finally understand why the whole

Look at the code:
Here Insert Picture Description
the method call actions in the following components inside, has been an error, unknown type of operation, you can own the store which clearly defines the thing. Hard to understand. . . .

  //this.$store.commit()触发--->mutaions
  //this.$store.dispatch()触发--->actions

I originally wrote this $ store.commit ( 'changeNum') has been given.
Then import {mapActions} from "vuex" ;
introduction mapActions

   methods: {
   changeNum(type) {
     if (type == "+") {
       this.add(); 
     } else {  
       this.sub();
     }
   },
   ...mapActions({
     add: "changeNumAsync",
     sub: "subtract"
   })
 },

Or write error

correct

  • 1. Code namespace commented
  • 2.this. $ Store.commit ( 'app / changeNum') or using mapActions
 ...mapActions({
    add: "app/changeNumAsync",
    sub: "app/subtract"
  })

The above method can be a second election, if the project data and more, is not recommended commented namespace, or method name will be repeated mess Well, if there namespaces, as long as the file name is not the same, the same way the name has nothing to Europe;
in short because not read or error actions inside the method, which is the type mentioned in the error due to the path of the course to solve it

Published 149 original articles · won praise 166 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_44369568/article/details/101625709