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

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

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

準備

為了說明這個問題,我們來一起實現(xiàn)一個小需求

即 現(xiàn)在有兩個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

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

那我們來看看 product 模塊

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

export default product

接著就是注冊這兩個模塊:

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'



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

//導出store對象
export default store

跨模塊調(diào)用state

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

打印一下

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

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

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

即: context.state -> 訪問的是 當前模塊下的 state

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

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

export default product

跨模塊調(diào)用getter

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

namespaced情況區(qū)分

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

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

首先來看默認情況 (沒有開啟namespaced)

官網(wǎng)說明:

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

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

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

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

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

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

...mapState({
    cartList: state => state.product.cartList,
    userInfo:state => state.user.userInfo,
}),
methods: {
    // 因為為默認情況,Action全局注冊,這里使用輔助函數(shù)直接取值
 ...mapActions( [
   'callsetuserInfo',
 ]),

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

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

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

啟命名空間的情況

官方文檔:

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

例如:

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

      // 模塊內(nèi)容(module assets)
      state: () => ({ ... }), // 模塊內(nèi)的狀態(tài)已經(jīng)是嵌套的了,使用 `namespaced` 屬性不會對其產(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']
          }
        },

        // 進一步嵌套命名空間
        posts: {
          namespaced: true,

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

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

modules: {
  foo: {
    namespaced: true,

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

    actions: {
      // 在這個模塊中, dispatch 和 commit 也被局部化了
      // 他們可以接受 `root` 屬性以訪問根 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 先 開啟命名空間 如下:

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

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

const product = {
	actions: {
		callsetuserInfo(context,payload) {
                  // 因為開啟了命名空間  訪問方法 需要通過模塊 user/setuserInfo  
           		context.dispatch('user/setuserInfo',{
				userInfo: '宇智波佐助'
			},{root: true})
		},
	}
}
export default product

在頁面中調(diào)用

 ...mapState({
    cartList: state => state.product.cartList,
    userInfo:state => state.user.userInfo,
}),
methods: {
    // 因為為開啟命名空間  不能直接訪問方法
    // 普通寫法
   ...mapActions([
   'product/callsetuserInfo',
 ]),  
    // 簡化寫法:   將模塊的空間名稱字符串作為第一個參數(shù)傳遞
 ...mapActions('product',[
   'callsetuserInfo',
 ]),
 // 如果想調(diào)用多個不同命名空間的方法
  ...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é)

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

相關(guān)文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    vue3+ts使用Echarts的實例詳解

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

最新評論

宜兰县| 綦江县| 松滋市| 洛南县| 玛沁县| 呼和浩特市| 柞水县| 本溪| 富蕴县| 甘谷县| 东兴市| 怀集县| 沂源县| 灵台县| 松滋市| 平乡县| 卢龙县| 南雄市| 陆良县| 沙坪坝区| 工布江达县| 杭锦后旗| 汽车| 遵化市| 沾益县| 什邡市| 三江| 东台市| 平塘县| 青河县| 卓尼县| 博爱县| 铜陵市| 东乡县| 灵台县| 吐鲁番市| 南岸区| 莱芜市| 榕江县| 宝丰县| 汕尾市|