zoukankan      html  css  js  c++  java
  • vue-vuex-mutations的基本使用

      之前说过,对state的修改必须经过mutations,而mutations中是用来定义方法的,在vue文件中通过提交某个方法来完成state的修改,比如说现在点击一个按钮,让counter+1,规范的做法如下:

      vue文件:

    <template>
      <div>
        <h1>我是首页页面</h1>
        <h2>{{$store.state.counter}}</h2>
        <button @click="addition">counter+1</button>
      </div>
    </template>
    
      methods: {
        addition(){
          this.$store.commit('increment')
        }
      }

      store下的index.js:

    const store = new Vuex.Store({
      state:{
        counter: 1000 
      },
      getters: {},
      mutations: {
        increment(state){
          state.counter++
        }
      },
      actions: {},
      modules: {}
    })

      要调用mutations中的方法,必须通过commit的方式来提交,参数是方法的名称。还有一个注意点的是,mutations中的方法默认参数1是state对象,可拿到state中的变量

  • 相关阅读:
    Python 异常处理
    Python File(文件) 方法
    python 文件定位
    globals() 和 locals() 函数
    python dir()函数
    python from…import* 语句
    python from…import 语句
    Python 模块
    python 匿名函数
    python 函数参数
  • 原文地址:https://www.cnblogs.com/ibcdwx/p/14639907.html
Copyright © 2011-2022 走看看