溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Vuex中常用知識(shí)點(diǎn)有哪些

發(fā)布時(shí)間:2021-01-21 10:02:34 來(lái)源:億速云 閱讀:158 作者:小新 欄目:編程語(yǔ)言

這篇文章給大家分享的是有關(guān)Vuex中常用知識(shí)點(diǎn)有哪些的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

vuex中常用的一些知識(shí)點(diǎn)

一、為什么要使用Vuex

1、多個(gè)組件依賴同一個(gè)狀態(tài),使用組件之間通信方法會(huì)非常繁瑣,例如多層嵌套組件。

2、需要全局保存的數(shù)據(jù),例如用戶、權(quán)限信息,全局系統(tǒng)設(shè)置

二、Vuex的五個(gè)核心屬性

1、state:存儲(chǔ)狀態(tài)

// store.jsconst store = new Vuex.Store({
  state: {
    count: 0
  }});// 組件里獲取count值$store.state.count

2、getters:state作為第一個(gè)參數(shù),其他getters作第二個(gè)參數(shù),返回值會(huì)根據(jù)他的依賴緩存起來(lái),相當(dāng)于Vue的計(jì)算屬性

// store.jsconst store = new Vuex.Store({
  state: {
    count: 1,
    sum: 2
  },
  getters: {
    getCountAndSum: (state,getters) => {
      return state.count + state.sum;
    }
  }});// 其他組件獲取getter$store.getters.getCountAndSum

3、mutations:修改狀態(tài)(同步的),state 作為第一個(gè)參數(shù),提交載荷作為第二個(gè)參數(shù)

const store = new Vuex.Store({
  state: {
    count: 1 
  },
  mutations: {
    increment (state, obj) {
      state.count += obj.n;
    }
  }});// 其他組件調(diào)用mutations的方法$store.commit('increment', {n: 100});

4、actions:異步操作(執(zhí)行mutations的方法,不是直變更狀態(tài))

const store = new Vuex.Store({
  state: {
    count: 1 
  },
  mutations: {
    increment (state, obj) {
      state.count += obj.n;
    }
  },
  actions: {
    increment (context) {
      context.commit('increment');
    }
  }});// 其他組件調(diào)用actions的方法$store.dispatch('increment');

5、modules:store的子模塊

const a = {
  state: {
    count: 0
  },
  getters: {
    getCount2 (state, getters, rootState) {
      return state.count + 2;
    }
  },
  mutations: {
    increment (state, getters, rootState) {
      state.count++;  
    }
  },
  actions: {
    increment (context) {
      // context.state.count;
      // context.rootState.count;
      context.commit('increment');
    }
  }};const b = {};const store = new Vuex.Store({
  modules: {
     a,
     b  }});// 其他組件調(diào)用 (獲取state的變量需要加上引入的module的別名)$store.state.a$store.state.b

三、Vuex輔助函數(shù)

<template>
  <div class="about">
    <h2>count: <span>{{count}}</span></h2>
    <h2>getCount: <span>{{$store.getters.getCount}}</span></h2>
    <h2>sum: <span>{{sum}}</span></h2>
    <h2>getSum: <span>{{$store.getters.getSum}}</span></h2>
    <button @click="clickB">test    </button>
  </div></template><script>import {mapState, mapGetters, mapMutations, mapActions} from 'vuex'; 
export default {
  name: 'about',
  data () {
    return {
      count: 0,
      sum: 0
    }
  },
  computed: {
    ...mapState({
      count: state => state.count,
      countAlias: 'count',
      countPlusLocalState (state) {
        return state.count + this.localCount;
      }
    }),
    ...mapGetters([
      'getCount', 'getSum'
    ])
  },
  mounted () {
    this.count = this.$store.state.count;
    this.sum = this.$store.state.a.sum;

  },
  methods:{
    ...mapMutations(
      'add','addO'
    ),
    ...mapActions([
      'add','addO'
    ]),
    clickB () {
      this.$store.dispatch('add');
      this.$store.dispatch('addO');
    }
  }
}</script>

感謝各位的閱讀!關(guān)于“Vuex中常用知識(shí)點(diǎn)有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI