码迷,mamicode.com
首页 > 其他好文 > 详细

3.vuex学习之mutations、mapMutations

时间:2018-05-29 19:40:26      阅读:1379      评论:0      收藏:0      [点我收藏+]

标签:AC   splay   loading   ext   getter   web   button   use   mil   

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>

 

3.vuex学习之mutations、mapMutations

标签:AC   splay   loading   ext   getter   web   button   use   mil   

原文地址:https://www.cnblogs.com/tw6668/p/9107409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!