首頁 > web前端 > Vue.js > 主體

Vuex中常用知識點(總結)

青灯夜游
發布: 2020-10-07 14:11:35
轉載
1735 人瀏覽過

Vuex中常用知識點(總結)

如果你之前使用過vue.js,你一定知道在vue中各個元件之間傳值的痛苦,在vue中我們可以使用vuex來保存我們需要管理的狀態值,下面我們就來看看vuex中常用的一些知識點,希望對大家有一定的幫助。

一、為什麼要使用Vuex

1、多個元件依賴同一個狀態,使用元件之間通訊方法會非常繁瑣,例如多層巢狀元件。

2、需要全域保存的數據,例如使用者、權限訊息,全域系統設定

#二、Vuex的五個核心屬性

1 、state:儲存狀態

// store.jsconst store = new Vuex.Store({
  state: {
    count: 0
  }});// 组件里获取count值$store.state.count
登入後複製

2、getters:state作為第一個參數,其他getters作第二個參數,回傳值會根據他的依賴快取起來,相當於Vue的計算屬性

// 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:修改狀態(同步的),state 作為第一個參數,提交載荷作為第二個參數

const store = new Vuex.Store({
  state: {
    count: 1 
  },
  mutations: {
    increment (state, obj) {
      state.count += obj.n;
    }
  }});// 其他组件调用mutations的方法$store.commit('increment', {n: 100});
登入後複製

4、actions:非同步操作(執行mutations的方法,不是直變更狀態)

const store = new Vuex.Store({
  state: {
    count: 1 
  },
  mutations: {
    increment (state, obj) {
      state.count += obj.n;
    }
  },
  actions: {
    increment (context) {
      context.commit('increment');
    }
  }});// 其他组件调用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  }});// 其他组件调用 (获取state的变量需要加上引入的module的别名)$store.state.a$store.state.b
登入後複製

三、Vuex輔助函數

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

  },
  methods:{
    ...mapMutations(
      &#39;add&#39;,&#39;addO&#39;
    ),
    ...mapActions([
      &#39;add&#39;,&#39;addO&#39;
    ]),
    clickB () {
      this.$store.dispatch(&#39;add&#39;);
      this.$store.dispatch(&#39;addO&#39;);
    }
  }
}</script>
登入後複製

#相關推薦:

2020年前端vue面試題大匯總(附答案)

vue教學推薦:2020最新的5個vue.js影片教學精選

更多程式相關知識,請造訪:程式設計入門! !

以上是Vuex中常用知識點(總結)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:jianshu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!