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>
  • 相关阅读:
    VC++导入导出类
    SVN操作手册
    stdcall与cdecl的区别
    VC++编译MPIR 2.7.0
    zabbix 监控 WEB 应用性能
    zabbix 常用监控模板
    zabbix 自定义 windows 监控项
    zabbix 自定义 nginx 监控模板
    zabbix agent 自定义 UserParameter
    zabbix python 微信告警脚本
  • 原文地址:https://www.cnblogs.com/tw6668/p/9107409.html
Copyright © 2011-2022 走看看