vuex-mutations

  • mutations中更改 Vuex  store 中的state的唯一方法是提交 mutations      

 this.$store.commit('事件名')     ///提交方法
在store组件中

... mutations: {
// 1.先声明要唤醒的函数,并放入ajax方法;此处state就是上边的,这样就可以state.allDatas直接赋值了; // 2.import axios from 'axios'引入 // 3.唤醒mutations:在mounted中唤醒:用store.commit('increment')方法; // increment为方法名 getAllDatas(state){ // var _this = this axios.get('http://127.0.0.1:8080/api/comments/') .then(function(response) { // console.log(response.data); // console.log(this) /* _this.$store.*/state.allDatas = response.data; }) .catch(function(error) { console.log(error); }); } }, ...

在App组件中(其他组件可以吗?)

...
mounted() {
      console.log(this);
     
            this.store.commit('getAllDatas');

    }
...
原文地址:https://www.cnblogs.com/hudaxian/p/14449823.html