Vue中mapMutations傳遞參數(shù)方式
通過子組件定義的方法傳遞參數(shù)
在…mapMutations引用
不多逼逼,看代碼!
store文件中:
import Vuex from 'vuex';
import Vue from 'vue';
Vue.use(Vuex);
let store = new Vuex.Store({
? ? state: {
? ? ? ? name: 'hahahah',
? ? ? ? age: '19',
? ? },
? ? mutations: {
? ? ? ? changeName(state, params) {
? ? ? ? ? ? console.log(params);
? ? ? ? ? ? state.name = params.name?
? ? ? ? },
? ? ? ? changeAge(state, params) {
? ? ? ? ? ? state.age = params.age
? ? ? ? }
? ? },
})
export default storeVueDemo中:
<template>
? <div>
? ? <h4>這里是son1組件</h4>
? ? name:{{name}}
? ? age:{{age}}
? ? <button @click="hehe">改名字</button>
? </div>
</template><script>
import { mapState, mapMutations } from "vuex";
export default {
? data() {
? ? return {
? ? ? list: {
? ? ? ? name: "6666"
? ? ? }
? ? };
? },
? computed: {
? ? ...mapState(["name", "age"])
? },
? methods: {
? ? hehe() {
? ? ? this.changeName(this.list);
? ? },
? ? ...mapMutations(["changeName"])
? }
};
</script>
<style>
</style>當(dāng)然也可以寫直接傳遞
state.age = params
<button @click="changeName(555555)">改名字</button>
省略data傳參
...mapMutations(["changeName"])
關(guān)于mapMutations的作用
mapMutations工具函數(shù)會(huì)將store中的commit方法映射到組件的methods中。和mapActions的功能幾乎一樣,我們來直接看它的實(shí)現(xiàn):
export function mapMutations (mutations) {
?? ?const res = {}
?? ?normalizeMap(mutations).forEach(({ key, val }) => {
?? ??? ?res[key] = function mappedMutation (...args) {
?? ??? ??? ?return this.$store.commit.apply(this.$store, [val].concat(args))
?? ??? ?}
?? ?})
?? ?return res
}函數(shù)的實(shí)現(xiàn)幾乎也和 mapActions 一樣,唯一差別就是映射的是 store 的 commit 方法。為了更直觀地理解,我們來看一個(gè)簡(jiǎn)單的例子:
import { mapMutations } from 'vuex'
export default {
?? ?// ...
?? ?methods: {
?? ??? ?...mapMutations([
?? ??? ??? ?'increment' // 映射 this.increment() 到 this.$store.commit('increment')
?? ??? ?]),
?? ??? ?...mapMutations({
?? ??? ??? ?add: 'increment' // 映射 this.add() 到 this.$store.commit('increment')
?? ??? ?})
?? ?}
}經(jīng)過mapMutations函數(shù)調(diào)用后的結(jié)果,如下所示:
import { mapActions } from 'vuex'
export default {
?? ?// ...
?? ?methods: {
?? ??? ?increment(...args) {
?? ??? ??? ?return this.$store.commit.apply(this.$store, ['increment'].concat(args))
?? ??? ?}
?? ??? ?add(...args) {
?? ??? ??? ?return this.$store.commit.apply(this.$store, ['increment'].concat(args))
?? ??? ?}
?? ?}
}以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue導(dǎo)入Echarts實(shí)現(xiàn)折線圖
這篇文章主要給大家介紹了關(guān)于vue+echarts實(shí)現(xiàn)折線圖的方法與注意事項(xiàng),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-12-12
vue components 動(dòng)態(tài)組件詳解
這篇文章主要介紹了vue components 動(dòng)態(tài)組件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11
使用proxytable 配置解決 vue-cli 的跨域請(qǐng)求問題【推薦】
這篇文章主要介紹了利用 proxytable 配置解決 vue-cli 的跨域請(qǐng)求問題,本文的目錄結(jié)構(gòu)基于 webpack 模板結(jié)構(gòu),需要的朋友可以參考下2018-05-05
vue3使用axios并封裝axios請(qǐng)求的詳細(xì)步驟
本篇文章分步驟給大家介紹了vue3使用axios并封裝axios請(qǐng)求的詳細(xì)步驟,結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友參考下吧2023-06-06
vue-cli腳手架打包靜態(tài)資源請(qǐng)求出錯(cuò)的原因與解決
這篇文章主要給大家介紹了關(guān)于vue-cli腳手架打包靜態(tài)資源請(qǐng)求出錯(cuò)的原因與解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue-cli具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
vue實(shí)現(xiàn)發(fā)表評(píng)論功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)發(fā)表評(píng)論功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
vue3使用xgPalyer實(shí)現(xiàn)截圖功能的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何在vue3中使用xgPalyer截圖功能,以及自定義插件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02

