最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vuex之module使用方法及場景說明

 更新時間:2023年10月26日 09:10:14   作者:綠足  
這篇文章主要介紹了Vuex之module使用方法及場景說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

一、module 使用場景

在項目開發(fā)過程中,隨著項目逐漸增大,數(shù)據(jù)關聯(lián)復雜度逐漸加大, 多人協(xié)同開發(fā),人員變動等。

我們會遇到vuex數(shù)據(jù)更新時,執(zhí)行某個action 導致同名/未預測到的關聯(lián)數(shù)據(jù)發(fā)生了變化。 

vue 基本思想之一便是數(shù)據(jù)驅(qū)動, vuex 更是專門的數(shù)據(jù)狀態(tài)關聯(lián)庫。

導致數(shù)據(jù)錯誤結(jié)果可想而知......

使用vuex module 命名空間概念則可以很好的解決這個問題?。?!

二、實例演練

先貼個demo

store.js

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
 
const test1 = {
  namespaced: true,
  state: {
    name: 'moduleA',
    type: 'module A'
  },
  mutations: {
    updateNameByMutation(state, appendStr){
      state.name = state.name + " append Str: " + appendStr
    }
  },
  actions: {
    udpateNameByAction({commit}, appendStr) {
      commit("updateNameByMutation", appendStr)
    }
  },
  getters: {
    getNameA(state){
      return state.name
    }
  }
}
const test2 = {
  // 當namespaced=true 時, vuex, 將會自動給各自module 添加訪問路徑名。 方便區(qū)分moduel
  namespaced: true,
  state:{
    name: 'moduleB',
    type: 'module B'
  },
  mutations: {
    updateNameByMutation(state, appendStr){
      state.name = state.name + " append Str: " + appendStr
    }
  },
  actions: {
    // 如果不使用命名空間, 那么view 指向actions 的該方法時,會執(zhí)行所有與指定action名相同的函數(shù)(即:這里module A,B 中該action都會執(zhí)行)
    udpateNameByAction({commit}, appendStr){
      commit("updateNameByMutation", appendStr)
    }
  },
  getters: {
    getNameB(state){
      return state.name
    }
  }
}
 
const storeInstall =  new Vuex.Store({
   state: {
     name: 'i am root state name'
   },
   modules:{
    // 這里的路徑名: test1, test2, 在view 中 通過 mapActions('test1', [actionName]) 使用并區(qū)分需要使用的module
    test1,
    test2
   }
})
 
export default storeInstall

store.js 幾個簡單的vuex 使用場景模擬。 我們有多個模塊,分別為: test1, test2...   。 

我們發(fā)現(xiàn)開發(fā)中可能會存在相同的stateName/ actionName/ mutaionName /。  (實際開發(fā)中,getterName 如果有重名編譯會提示 getter 重名....)

我們使用vuex 需要實例化一個Vuex的Store構(gòu)造函數(shù)。 這里storeInstall 中第一個state, 我們可以理解為根 state, 它全局可訪問。 modules 中則是我們自定義注冊的module. 每個module 中都有自己獨立的state, action, mutation, getter...  

需要注意的是,這里通過給每個module 對象添加namespaced: true, 來達到命名空間來區(qū)分Module的效果。也是通過它來區(qū)分更新/調(diào)用 對應的vuex 方法來隔離未知數(shù)據(jù)更新等數(shù)據(jù)相關問題。

Test.vue

<template>
  <div>
    <div>
    <h2>Page Test1</h2>
    </div>
    <div>
    <a href="javascript:" rel="external nofollow"  rel="external nofollow"  @click="changeName">udpate: 名稱Name</a>  &nbsp; &nbsp;
    <a href="javascript:" rel="external nofollow"  rel="external nofollow"  @click="showName">顯示更新后的Name</a> &nbsp; &nbsp;
    </div>
  </div>
</template>
<script>
import { mapState, mapActions } from 'vuex'
export default {
  data(){
    return {}
  },
  computed: {
    ...mapState('test1', {
      state: state => state
    })
  },
  methods: {
    // test1 模塊路徑名
    ...mapActions('test1', [
      'udpateNameByAction'
    ]),
    changeName(){
      this["udpateNameByAction"]('ha ha test1 udpate !!')
    },
    showName(){
      console.log(this.$store.state)
    },
  },
  mounted() {
    console.log("store name: ", this.$store)
    console.log("namespace test1 state: ", this.state)
  }
}
</script>
 

這個test1.vue (還有另外一個對應的test2.vue 代碼基本一樣,主要用來區(qū)別兩個頁面vuex的更新效果), 則向我們展示了如何使用vuex 提供的api 來做數(shù)據(jù)更新(這里介紹的是引入命名空間的module 場景)。       

(1) 首先通過vuex 引入需要的api 這里主要演示 ...mapActions .         

(2)  在module場景下引入mapActions 我們發(fā)現(xiàn),第一個參數(shù)傳的是 module 路徑(就引入各個module的名稱)名。 這種方式將只會在當前view中,導出指定模塊下注冊的 action 集合。         

(3)  當調(diào)用指定定module下的action 執(zhí)行state 更新操作時, vuex 通過該module名找到對一定的action 進行下一步mutation 操作。 同時受到影響state的也只會時該命名空間下的state 

三、Vuex module namespaced

我們來討論下module 設置了namespaced 與不設置vuex store 中的module數(shù)據(jù)有何區(qū)別?

打字累截個圖吧,一目了然:

圖1: 沒有設置namespaced=true 

 

圖2: 設置namespaced=true 

 

總結(jié)

關于vuex module 這里只是個基本講解。

總結(jié)下來就是module  給了我們一種隔離vuex store 各個 state及相關api 的方法,讓數(shù)據(jù)相關操作在復雜的項目場景可以更清晰,易追蹤。 

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論

杭锦后旗| 七台河市| 靖远县| 永嘉县| 错那县| 定州市| 沙湾县| 南皮县| 青海省| 安多县| 四川省| 广州市| 文昌市| 微山县| 庄河市| 迁安市| 梁平县| 缙云县| 台南县| 潢川县| 忻州市| 梓潼县| 连云港市| 和平区| 稻城县| 资溪县| 延庆县| 儋州市| 交口县| 蓬安县| 巴彦淖尔市| 长顺县| 扶余县| 石嘴山市| 施甸县| 永和县| 镇平县| 青海省| 巴彦淖尔市| 昌吉市| 平阴县|