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

Vuex模塊化用法?跨模塊調(diào)用的方式

 更新時(shí)間:2023年10月26日 09:22:47   作者:小楊闖關(guān)之情迷代碼  
這篇文章主要介紹了Vuex模塊化用法?跨模塊調(diào)用的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,

準(zhǔn)備

為了說(shuō)明這個(gè)問(wèn)題,我們來(lái)一起實(shí)現(xiàn)一個(gè)小需求

即 現(xiàn)在有兩個(gè)module - productuser

需求為調(diào)用 product 模塊的方法 去修改 user 模塊的 userInfo(用戶名信息)

// module user 模塊
const user = {
	state: {
		userInfo: '鳴人', // 用戶信息
	},
	mutations:{
		SET_UserInfo(state,payload) {
			state.userInfo = payload.userInfo
		},
	},
	actions: {
		setuserInfo(context,payload) {
			return new Promise(resolve => {
				context.commit('SET_UserInfo',payload)
				resolve();
			})
		},
	}
}

export default user

不難看出, 上述需求 其實(shí)就是在 product 模塊的方法中的去調(diào)用 user模塊 的 setuserInfo 方法

那我們來(lái)看看 product 模塊

const product = {
	actions: {
		callsetuserInfo(context,payload) {
                // 在這里調(diào)用 user 模塊的 setuserInfo 方法
		},
	}
}

export default product

接著就是注冊(cè)這兩個(gè)模塊:

import Vue from 'vue'
//引用Vuex
import Vuex from 'vuex'
Vue.use(Vuex)

import product from '@/store/modules/product'
import user from '@/store/modules/user'
// import  getters from '@/store/modules/getters.js'
// import  actions from '@/store/modules/actions.js'



//實(shí)例store對(duì)象
const store = new Vuex.Store({
	modules: {
		product,
		user
		// getters,
		// actions
	}
})

//導(dǎo)出store對(duì)象
export default store

跨模塊調(diào)用state

這里我們首先要了解 方法中的第一個(gè)參數(shù) context

打印一下

發(fā)現(xiàn)有 commit,dispatch,getters,rootGetters,rootState 幾個(gè)參數(shù)

結(jié)合官網(wǎng)說(shuō)明:

同樣,對(duì)于模塊內(nèi)部的 action,局部狀態(tài)通過(guò) context.state 暴露出來(lái),根節(jié)點(diǎn)狀態(tài)則為 context.rootState

即: context.state -> 訪問(wèn)的是 當(dāng)前模塊下的 state

context.rootState -> 是根節(jié)點(diǎn)狀態(tài)

const product = {
	actions: {
		callsetuserInfo(context,payload) {                
                // 通過(guò) 根節(jié)點(diǎn)狀態(tài) 去訪問(wèn) user 模塊的 userInfo信息
                console.log(context.rootState.user.userInfo)  // '鳴人'
		},
	}
}

export default product

跨模塊調(diào)用getter

和跨模塊調(diào)用state類似 ,通過(guò) context.rootGetters去訪問(wèn)模塊

namespaced情況區(qū)分

跨模塊調(diào)用mutation,action

這里要區(qū)分 模塊是否開(kāi)啟了命名空間 namespaced

首先來(lái)看默認(rèn)情況 (沒(méi)有開(kāi)啟namespaced)

官網(wǎng)說(shuō)明:

  • 默認(rèn)情況下,模塊內(nèi)部的 action 和 mutation 仍然是注冊(cè)在全局命名空間的——這樣使得多個(gè)模塊能夠?qū)ν粋€(gè) action 或mutation 作出響應(yīng)。
  • Getter同樣也默認(rèn)注冊(cè)在全局命名空間,但是目前這并非出于功能上的目的(僅僅是維持現(xiàn)狀來(lái)避免非兼容性變更)。
  • 必須注意,不要在不同的、無(wú)命名空間的模塊中定義兩個(gè)相同的getter 從而導(dǎo)致錯(cuò)誤。

跨模塊調(diào)用actions:

// 三個(gè)參數(shù): 1. 模塊/方法名 2.參數(shù) 3.是否從跟節(jié)點(diǎn)尋找
context.dispatch('需要調(diào)用的方法名',params,{root: true})

跨模塊調(diào)用mutations:

// 三個(gè)參數(shù): 1. 模塊/方法名 2.參數(shù) 3.是否從跟節(jié)點(diǎn)尋找
context.commit('需要調(diào)用的方法名',params,{root: true})
const product = {
	actions: {
		callsetuserInfo(context,payload) {
                  // 因?yàn)槭悄J(rèn)為全局注冊(cè)  可以直接訪問(wèn) setuserInfo  
                  //  {root: true} 表明從根節(jié)點(diǎn)尋找  如果不加 則是從當(dāng)前模塊中
                  //  這里即 product模塊 去找 setuserInfo 方法 (當(dāng)然找不到)
           		contxt.dispatch('setuserInfo',{
				userInfo: '宇智波佐助'
			},{root: true})
		},
	}
}
export default product

在頁(yè)面中調(diào)用:

...mapState({
    cartList: state => state.product.cartList,
    userInfo:state => state.user.userInfo,
}),
methods: {
    // 因?yàn)闉槟J(rèn)情況,Action全局注冊(cè),這里使用輔助函數(shù)直接取值
 ...mapActions( [
   'callsetuserInfo',
 ]),

async onLoad(params) {
    this.callsetuserInfo().then(()=> {
        // 打印user 模塊 的 userInfo
        console.log(this.userInfo) //  宇智波佐助 
    })
}

打印設(shè)置值- 宇智波佐助 ,默認(rèn)情況(未開(kāi)啟命名空間)跨模塊調(diào)用actions 成功

跨模塊調(diào)用mutations類似,語(yǔ)法糖改為 context.commit 即可

啟命名空間的情況

官方文檔:

  • 如果希望你的模塊具有更高的封裝度和復(fù)用性,你可以通過(guò)添加 namespaced: true的方式使其成為帶命名空間的模塊。
  • 當(dāng)模塊被注冊(cè)后,它的所有 getter、action 及 mutation都會(huì)自動(dòng)根據(jù)模塊注冊(cè)的路徑調(diào)整命名。

例如:

const store = createStore({
  modules: {
    account: {
      namespaced: true,

      // 模塊內(nèi)容(module assets)
      state: () => ({ ... }), // 模塊內(nèi)的狀態(tài)已經(jīng)是嵌套的了,使用 `namespaced` 屬性不會(huì)對(duì)其產(chǎn)生影響
      getters: {
        isAdmin () { ... } // -> getters['account/isAdmin']
      },
      actions: {
        login () { ... } // -> dispatch('account/login')
      },
      mutations: {
        login () { ... } // -> commit('account/login')
      },

      // 嵌套模塊
      modules: {
        // 繼承父模塊的命名空間
        myPage: {
          state: () => ({ ... }),
          getters: {
            profile () { ... } // -> getters['account/profile']
          }
        },

        // 進(jìn)一步嵌套命名空間
        posts: {
          namespaced: true,

          state: () => ({ ... }),
          getters: {
            popular () { ... } // -> getters['account/posts/popular']
          }
        }
      }
    }
  }
})

官網(wǎng)中的 在帶命名空間的模塊內(nèi)訪問(wèn)全局內(nèi)容 的例子:

modules: {
  foo: {
    namespaced: true,

    getters: {
      // 在這個(gè)模塊的 getter 中,`getters` 被局部化了
      // 你可以使用 getter 的第四個(gè)參數(shù)來(lái)調(diào)用 `rootGetters`
      someGetter (state, getters, rootState, rootGetters) {
        getters.someOtherGetter // -> 'foo/someOtherGetter'
        rootGetters.someOtherGetter // -> 'someOtherGetter'
        rootGetters['bar/someOtherGetter'] // -> 'bar/someOtherGetter'
      },
      someOtherGetter: state => { ... }
    },

    actions: {
      // 在這個(gè)模塊中, dispatch 和 commit 也被局部化了
      // 他們可以接受 `root` 屬性以訪問(wèn)根 dispatch 或 commit
      someAction ({ dispatch, commit, getters, rootGetters }) {
        getters.someGetter // -> 'foo/someGetter'
        rootGetters.someGetter // -> 'someGetter'
        rootGetters['bar/someGetter'] // -> 'bar/someGetter'

        dispatch('someOtherAction') // -> 'foo/someOtherAction'
        dispatch('someOtherAction', null, { root: true }) // -> 'someOtherAction'

        commit('someMutation') // -> 'foo/someMutation'
        commit('someMutation', null, { root: true }) // -> 'someMutation'
      },
      someOtherAction (ctx, payload) { ... }
    }
  }
}

好了,了解之后 ?;氐轿覀兊男枨?= =!

模塊user,product 先 開(kāi)啟命名空間 如下:

const user = {
	namespaced: true,
     // 后續(xù)代碼
 }

在product模塊 中去調(diào)用user 模塊的action方法

const product = {
	actions: {
		callsetuserInfo(context,payload) {
                  // 因?yàn)殚_(kāi)啟了命名空間  訪問(wèn)方法 需要通過(guò)模塊 user/setuserInfo  
           		context.dispatch('user/setuserInfo',{
				userInfo: '宇智波佐助'
			},{root: true})
		},
	}
}
export default product

在頁(yè)面中調(diào)用

 ...mapState({
    cartList: state => state.product.cartList,
    userInfo:state => state.user.userInfo,
}),
methods: {
    // 因?yàn)闉殚_(kāi)啟命名空間  不能直接訪問(wèn)方法
    // 普通寫法
   ...mapActions([
   'product/callsetuserInfo',
 ]),  
    // 簡(jiǎn)化寫法:   將模塊的空間名稱字符串作為第一個(gè)參數(shù)傳遞
 ...mapActions('product',[
   'callsetuserInfo',
 ]),
 // 如果想調(diào)用多個(gè)不同命名空間的方法
  ...mapActions('模塊B',[
   '模塊B Function',
 ]),
async onLoad(params) {
    this.callsetuserInfo().then(()=> {
        // 打印user 模塊 的 userInfo
        console.log(this.userInfo) //  宇智波佐助 
    })
}

參考文檔: https://vuex.vuejs.org/zh/guide/modules.html

總結(jié)

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

相關(guān)文章

  • 教你如何編寫Vue.js的單元測(cè)試的方法

    教你如何編寫Vue.js的單元測(cè)試的方法

    這篇文章主要介紹了教你如何編寫Vue.js的單元測(cè)試的方法,介紹了簡(jiǎn)單的單元測(cè)試,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • vue實(shí)現(xiàn)拖拽或點(diǎn)擊上傳圖片

    vue實(shí)現(xiàn)拖拽或點(diǎn)擊上傳圖片

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)拖拽或點(diǎn)擊上傳圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Vue實(shí)現(xiàn)全局異常處理的幾種方案

    Vue實(shí)現(xiàn)全局異常處理的幾種方案

    本文主要介紹了使用pyscript在網(wǎng)頁(yè)中撰寫Python程式的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • Vue3初探之ref、reactive以及改變數(shù)組的值

    Vue3初探之ref、reactive以及改變數(shù)組的值

    在setup函數(shù)中,可以使用ref函數(shù)和reactive函數(shù)來(lái)創(chuàng)建響應(yīng)式數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Vue3初探之ref、reactive以及改變數(shù)組值的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • vue?使用mescroll.js框架實(shí)現(xiàn)下拉加載和上拉刷新功能

    vue?使用mescroll.js框架實(shí)現(xiàn)下拉加載和上拉刷新功能

    這篇文章主要介紹了vue?使用mescroll.js框架?實(shí)現(xiàn)下拉加載和上拉刷新功能,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • Vue實(shí)現(xiàn)數(shù)值型輸入框并限制長(zhǎng)度

    Vue實(shí)現(xiàn)數(shù)值型輸入框并限制長(zhǎng)度

    這篇文章主要介紹了Vue實(shí)現(xiàn)數(shù)值型輸入框并限制長(zhǎng)度,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • vue中組件的name屬性含義和用法示例

    vue中組件的name屬性含義和用法示例

    組件是有name屬性的,匹配的就是組件的name,下面這篇文章主要給大家介紹了關(guān)于vue中組件的name屬性含義和用法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • vue 實(shí)現(xiàn)超長(zhǎng)文本截取,懸浮框提示

    vue 實(shí)現(xiàn)超長(zhǎng)文本截取,懸浮框提示

    這篇文章主要介紹了vue 實(shí)現(xiàn)超長(zhǎng)文本截取,懸浮框提示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • vue3使用vue-router及路由權(quán)限攔截方式

    vue3使用vue-router及路由權(quán)限攔截方式

    這篇文章主要介紹了vue3使用vue-router及路由權(quán)限攔截方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue3+ts使用Echarts的實(shí)例詳解

    vue3+ts使用Echarts的實(shí)例詳解

    這篇文章主要介紹了vue3+ts使用Echarts的實(shí)例詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03

最新評(píng)論

巴林左旗| 客服| 庄浪县| 临武县| 嘉荫县| 黑山县| 灌阳县| 仁怀市| 都江堰市| 温宿县| 珲春市| 封丘县| 边坝县| 小金县| 海南省| 盐亭县| 华蓥市| 永安市| 崇明县| 黑山县| 南溪县| 扎赉特旗| 泽库县| 普格县| 和龙市| 聂荣县| 龙川县| 泌阳县| 宜兴市| 陆川县| 盐城市| 岑溪市| 西和县| 大悟县| 乌拉特后旗| 北京市| 泗洪县| 彭州市| 扶余县| 康定县| 昂仁县|