zoukankan      html  css  js  c++  java
  • 3.vuex学习之mutations、mapMutations

    mutations状态更改

      在mutations对象中创建更改state状态的方法。mapMutations是vuex提供的辅助函数

    在store.js中

    import Vue from 'vue'
    import Vuex from 'vuex'
    
    Vue.use(Vuex)
    
    //访问状态对象
    const state = {
        count:1
    }
    
    // 触发的状态,mutations是同步的
    const mutations = {
        jian(state){//传入一个state对象
            state.count--
        },
        jia(state,n){
            state.count+=n.a
        }
    }
    
    
    export default new Vuex.Store({
        state,
        mutations
    })

    在App.vue文件中

    <template>
      <div id="app">
        
        <img src="./assets/logo.png">
        <h1>{{ msg}}</h1>
        <!--访问状态对象-->
        <div>使用mutation触发改变状态-->{{$store.state.count}}</div>
        <!--用commit访问触发的mutations里面的方法-->
        <button @click="$store.commit('jia',{a:10})">+</button>
        <!--使用mapMutations-->
        <button @click="jian">-</button>
      </div>
    </template>
    
    <script>
    
    //vuex提供的辅助函数
    import {mapState,mapMutations,mapGetters,mapActions} from 'vuex'
    export default {
      name: 'app',
      data () {
        return {
          msg: 'Welcome to Your Vue.js App'
        }
      },
      
      //解决页面白屏问题
      mounted(){
        if(app.hasChildNodes()){
              loading.style.display="none";
           }else{
             console.log(app.childNodes)
           }
      },
    
      // methods:mapMutations([
      //   'jian'
      // ])
      methods:{
        ...mapMutations([//
          'jian'
        ])
      }
    }
    </script>
    
    <style>
    #app {
      font-family: 'Avenir', Helvetica, Arial, sans-serif;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      text-align: center;
      color: #2c3e50;
      margin-top: 60px;
    }
    
    h1, h2 {
      font-weight: normal;
    }
    
    ul {
      list-style-type: none;
      padding: 0;
    }
    
    li {
      display: inline-block;
      margin: 0 10px;
    }
    
    a {
      color: #42b983;
    }
    </style>
  • 相关阅读:
    Loadrunner的Tuxedo脚本分析,基本流程和基本函数介绍
    安装ArcGIS Server 9.2的一些建议
    .obj,.lib,.dll,.h之间的相互关系
    中国Albers 投影参数设置参考资料
    投影常识
    vc++2005环境中静态调用DLL(隐式)
    设置GDAL_DATA环境变量
    开源代码搜索利器Koders
    更正GDAL_DATA设置一文错误
    2007年的元宵节
  • 原文地址:https://www.cnblogs.com/tw6668/p/9107409.html
Copyright © 2011-2022 走看看