1. 程式人生 > >3.vuex學習之mutations、mapMutations

3.vuex學習之mutations、mapMutations

methods -- ext ans src AD nodes mounted efault

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