Vuex

Vuex

1、State單一狀態(tài)數(shù)
//store/index.js
const store = new Vuex.Store({
    state:{
        counter:200
    },
    
})
//app.vue
<h2>{{$store.state.counter}}</h2>
//200
2、getters基本使用

獲取state變異后的狀態(tài)

//store/index.js
const store = new Vuex.Store({
    state:{
        counter:200
    },
    getters:{
        qCounter(state){
            return state.counter * state.counter
        }
    }
})
//app.vue
<h2>{{$store.getters.qCounter}}</h2>
//40000
  • getters傳參
const store = new Vuex.Store({
    state:{
        counter:200,
        students:[
            {id:110,name:"xxx1",age:10},
            {id:111,name:"xxx2",age:15},
            {id:112,name:"xxx3",age:20},
            {id:113,name:"xxx4",age:25}
        ]
    },
    getters:{
        qCounter(state){
            return state.counter * state.counter
        },
        more20stu(state){
            return state.students.filter(s => s.age >=20 )
        },
        more20stuLenght(state,getters){
            return getters.more20stu.length
        },
        moreAgeStu(state){
            return function(age){
                return state.students.filter(s => s.age >= age)
            }
        }
    }
})

//app.vue
    <h2>{{$store.getters.qCounter}}</h2>
    <p>{{$store.getters.more20stu}}</p>
    <p>{{$store.getters.more20stuLenght}}</p>
    <p>{{$store.getters.moreAgeStu(15)}}</p>
image.png
3、mutations基本使用

Vuex的store狀態(tài)的更新唯一方式:提交mutation

//store/index.js
const store = new Vuex.Store({
    state:{
        counter:200
    },
    mutations:{
        // 方法
        increment(state){
            state.counter++
        },
        decrement(state){
            state.counter--
        }
    }
})

//app.vue
    <button @click="addclick">+</button>
    <button @click="subclick">-</button>

export default {
  name: 'App',
  components:{
    HelloTest
  },
  data(){
    return {
    }
  },
  methods:{
    addclick(){
      this.$store.commit('increment')
    },
    subclick(){
      this.$store.commit('decrement')
    }
  }
}

  • mutations 傳參
//store/index.js
const store = new Vuex.Store({
    mutations:{
        // 方法
        incrementCount(state,count){
            state.counter += count
        }
    }
})

//app.vue
export default {
  methods:{
    addcount(count){
      this.$store.commit('incrementCount',count)
    }
  }
}
<h2>{{$store.state.counter}}</h2>
<button @click="addcount(5)">+5</button>

mutaition 不可進(jìn)行異步操作

4、Action的基本定義

用來(lái)替代mutation進(jìn)行異步操作

//app.vue
<h2>{{$store.state.info}}</h2>
<button @click="updataInfo">修改信息</button>

export default {
    methods:{
    updataName(){
      this.$store.commit('updataName',"lili")
    }
  }
}
// index.js
const store = new Vuex.Store({
  mutations:{
        // 方法
        updateInfo(state){
            state.info.name ="lxz0026"
        }
    },
    actions:{
        aUpdataInfo(context,payload){
            return new Promise((resolve ,reject) => {
                setTimeout(()=>{
                    context.commit('updateInfo');
                    console.log(payload);
                    resolve("111")
                },1000)
            })
            
        }
    }
})
5、Modules的基本定義

模塊化

//moduleA.js

export default {
    state:{
        name:"moduleA"
    },
    mutations:{
        updataName(state,payload){
            state.name = payload;
        }
    }
}

//index.js
import moduleA from './modules/moduleA'
const store = new Vuex.Store({
    state,
    mutations,
    actions,
    getters,
    modules:{
        a:moduleA
    }
})

//app.vue
<h2>{{$store.state.a.name}}</h2>
<button @click="updataName">module修改名字</button>

<script>
export default {
  methods:{
    updataName(){
      this.$store.commit('updataName',"lili")
    }
  }
}
</script>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ### store 1. Vue 組件中獲得 Vuex 狀態(tài) ```js //方式一 全局引入單例類 // 創(chuàng)建一...
    蕓豆_6a86閱讀 794評(píng)論 0 3
  • Vuex是什么? Vuex 是一個(gè)專為 Vue.js應(yīng)用程序開發(fā)的狀態(tài)管理模式。它采用集中式存儲(chǔ)管理應(yīng)用的所有組件...
    蕭玄辭閱讀 3,242評(píng)論 0 6
  • 安裝 npm npm install vuex --save 在一個(gè)模塊化的打包系統(tǒng)中,您必須顯式地通過(guò)Vue.u...
    蕭玄辭閱讀 3,053評(píng)論 0 7
  • Vuex 是一個(gè)專為 Vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理模式。它采用集中式存儲(chǔ)管理應(yīng)用的所有組件的狀態(tài),并以相應(yīng)...
    白水螺絲閱讀 4,809評(píng)論 7 61
  • Vuex的五個(gè)核心概念 本文參考自Vue文檔,說(shuō)的非常詳細(xì),建議看文檔。 Vuex是什么? VueX 是一個(gè)專門為...
    一二三四五_6ce3閱讀 1,374評(píng)論 0 0

友情鏈接更多精彩內(nèi)容