国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

前端vue入門(純代碼)22_四個map

這篇具有很好參考價值的文章主要介紹了前端vue入門(純代碼)22_四個map。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

女為悅己者容,男為悅己者窮。?。?!

【23.Vuex中的四個map方法】

[可以去官網(wǎng)看看Vuex3文檔](Vuex 是什么? | Vuex (vuejs.org))

1.vuex求和案例—getters版

  • getters概念:當state中的數(shù)據(jù)需要經(jīng)過加工后再使用時,可以使用getters加工?!臼?store 的計算屬性】
  • 【getters理解】:類似是Vuex中的計算屬性,也具有緩存功能:如果state中的數(shù)據(jù)得到了改變,那么getters中的屬性的值也會發(fā)生改變,如果state的值沒有發(fā)生任何變化,那么getters中的屬性,的值就不會發(fā)生改變。

store/index.js

  • getters中配置的屬性bigSum(a,b),必須傳一個參數(shù):state【這樣方便我們對vuex中的共享數(shù)據(jù)進行操作】;第二個參數(shù):getters【可傳,可不傳】
  • 前端vue入門(純代碼)22_四個map,Vue前端,前端,vue.js,javascript
//準備getters——用于將state中的數(shù)據(jù)進行加工【配置定義getters】
const getters = {
	bigSum(state) {
    console.log('getters第一個參數(shù):',state);
    return state.sum*10
  },
};
//準備state——用于存儲數(shù)據(jù)
//數(shù)據(jù),相當于data
const state = {
	sum: 0, //當前的和
};

//創(chuàng)建并暴露store
export default new Vuex.Store({
	// property 簡寫 (用在對象某個 property 的 key 和被傳入的變量同名時)
	actions,
	mutations,
	state,
  // 在store中聲明getters
  getters,
});

效果展示:

前端vue入門(純代碼)22_四個map,Vue前端,前端,vue.js,javascript

2.vuex求和案例—mapState和mapGetters版

1.mapGetters 輔助函數(shù)

將 store 中的 getters 映射到局部計算屬性。

  • mapGetters方法:用于幫助我們映射getters中的數(shù)據(jù)為計算屬性

導入mapGetters

//幫助我們將store中的getters部分,映射到本組件中使用。
import { mapGetters } from 'vuex';

使用mapGetters的組件中

computed: {
    //借助mapGetters生成計算屬性:bigSum(對象寫法)
    ...mapGetters({bigSum:'bigSum'}),

    //借助mapGetters生成計算屬性:bigSum(數(shù)組寫法)
    ...mapGetters(['bigSum'])
},
  • 詳細解讀一下代碼:...mapGetters({bigSum:'bigSum'})
  1. 【…xxx】:對象運算符

  2. mapGetters({bigSum:'bigSum'})】:就是下面代碼的簡寫形式

bigSum(){
    return this.$store.getters.bigSum
}, 
  1. mapGetters({bigSum:'bigSum'})】:不能簡寫成【mapGetters({bigSum})或mapGetters(){'bigSum'})

  2. {bigSum:'bigSum'}名字一樣的話,可以采用數(shù)組的方式簡寫:...mapGetters(['bigSum'])

2.mapState 輔助函數(shù)

將 store 中的 state 映射到局部計算屬性。

  • mapState方法:用于幫助我們映射state中的數(shù)據(jù)為計算屬性

導入mapState

//幫助我們將store中的state部分,映射到本組件中使用。
import { mapGetters,mapState } from 'vuex';

使用mapState的組件中

computed: {
    //借助mapState生成計算屬性,從state中讀取數(shù)據(jù)。(對象寫法)
	...mapState({sum:'sum',Sjob:'Sjob',Sname:'Sname'}),

	//借助mapState生成計算屬性,從state中讀取數(shù)據(jù)。(數(shù)組寫法)
    ...mapState(['sum','Sjob','Sname']),  
},
  • 詳細解讀一下代碼:...mapState({sum:'sum',Sjob:'Sjob',Sname:'Sname'})
  1. 【…xxx】:對象運算符

  2. mapState({sum:'sum',Sjob:'Sjob',Sname:'Sname'})】:就是下面代碼的簡寫形式

sum(){
  return this.$store.state.sum
},
Sjob(){
  return this.$store.state.Sjob
},
Sname(){
  return this.$store.state.Sname
}, 
  1. {sum:'sum',...}名字一樣的話,可以采用數(shù)組的方式簡寫:...mapState(['sum','Sjob','Sname'])

vuex求和案例—mapState和mapGetters版完整代碼:

Count.vue文章來源地址http://www.zghlxwxcb.cn/news/detail-527730.html

<template>
	<div>
		<h1>當前求和為:{{ sum }}</h1>
		<h3 style="color: red">當前求和放大10倍為:{{ bigSum }}</h3>
		<h3>我叫{{Sname}},是一名{{ Sjob }}</h3>
		<select v-model.number="n">
			<option value="1">1</option>
			<option value="2">2</option>
			<option value="3">3</option>
		</select>
		<button @click="increment">+</button>
		<button @click="decrement">-</button>
		<button @click="incrementOdd">當前求和為奇數(shù)再加</button>
		<button @click="incrementWait">等一等再加</button>
	</div>
</template>

<script>
import { mapGetters,mapState } from 'vuex';
export default {
	name: 'Count',
	data() {
		return {
			n: 1,
		};
	},
	computed: {
		//#region
		// 1.不借助mapGetters
		/* 		bigSum() {
			// console.log(this);
			return this.$store.getters.bigSum;
		}, */

		//借助mapGetters生成計算屬性,從getters中讀取數(shù)據(jù)。(對象寫法)
		// ...mapGetters({bigSum:'bigSum'})

		//借助mapGetters生成計算屬性,從getters中讀取數(shù)據(jù)。(數(shù)組寫法)
		...mapGetters(['bigSum']),
		//#endregion

		//#region
		// 2.不借助mapState
      /*sum(){
          return this.$store.state.sum
        },
        Sjob(){
          return this.$store.state.Sjob
        },
        Sname(){
          return this.$store.state.Sname
        }, */

        //借助mapState生成計算屬性,從state中讀取數(shù)據(jù)。(對象寫法)
        // ...mapState({sum:'sum',Sjob:'Sjob',Sname:'Sname'}),

	   //借助mapState生成計算屬性,從state中讀取數(shù)據(jù)。(數(shù)組寫法)
       ...mapState(['sum','Sjob','Sname']),
		//#endregion
	},
	methods: {
		increment() {
		// 組件派發(fā)【dispatch】任務到actions,然后在actions中觸發(fā)mutations中的方法
			this.$store.commit('JIA', this.n);
		},
		decrement() {
			this.$store.commit('JIAN', this.n);
		},
		incrementOdd() {
			this.$store.dispatch('jiaOdd', this.n);
		},
		incrementWait() {
			this.$store.dispatch('jiaWait', this.n);
		},
	},
	mounted() {
		console.log('Count', this.$store);
	},
};
</script>

<style scoped>
button {
	margin-left: 10px;
}
select {
	margin-left: 20px;
}
</style>

store/index.js

//該文件用于創(chuàng)建Vuex中最為核心的store
import Vue from 'vue';
//引入Vuex
import Vuex from 'vuex';
//應用Vuex插件
Vue.use(Vuex);

//準備actions——用于響應組件中的動作【接收組件派發(fā)過來的方法】
const actions = {
	jiaOdd(context, value) {
		console.log('actions中的jiaOdd被調(diào)用了');
		// 這里可以訪問到state里存儲的數(shù)據(jù) sum
		if (context.state.sum % 2) {
			context.commit('JIA', value);
		}
	},

	jiaWait(context, value) {
		console.log('actions中的jiaWait被調(diào)用了');
		setTimeout(() => {
			context.commit('JIA', value);
		}, 500);
	},
};
//準備mutations——用于操作數(shù)據(jù)(state)
const mutations = {
	JIA(state, value) {
		console.log('mutations中的JIA被調(diào)用了');
		state.sum += value;
	},

	JIAN(state, value) {
		console.log('mutations中的JIAN被調(diào)用了');
		state.sum -= value;
	},
};
//準備getters——用于將state中的數(shù)據(jù)進行加工【配置定義getters】
const getters = {
	bigSum(state,getters) {
    // console.log('getters第一個參數(shù):',state);
    // console.log('getters第二個參數(shù):',getters);
    return state.sum*10
  },
};
//準備state——用于存儲數(shù)據(jù)
//數(shù)據(jù),相當于data
const state = {
	sum: 0, //當前的和
  Sname:'伍六七',
  Sjob:'理發(fā)師',
};

//創(chuàng)建并暴露store
export default new Vuex.Store({
	// property 簡寫 (用在對象某個 property 的 key 和被傳入的變量同名時)
	actions,
	mutations,
	state,
  // 在store中聲明getters
  getters,
});

3.vuex求和案例—mapMutations和mapActions版

1.mapMutations 輔助函數(shù)

將 store 中的 mutations 映射到局部方法。

  • mapMutations方法:用于幫助我們在組件中生成與mutations對話的方法,即:包含$store.commit(xxx)的函數(shù)

導入mapMutations

//幫助我們將store中的mutations部分,映射到本組件中使用。
import { mapMutations } from 'vuex';

使用mapMutations的組件中

methods:{
    //借助mapMutations生成對應的方法,方法中會調(diào)用commit去聯(lián)系mutations(對象寫法)
    ...mapMutations({ increment: 'JIA', decrement: 'JIAN' }),
    //借助mapMutations生成對應的方法,方法中會調(diào)用commit去聯(lián)系mutations(數(shù)組寫法)
    ...mapMutations([ 'JIA', 'JIAN' ]),
}
  • 詳細解讀一下代碼:...mapMutations({ increment: 'JIA', decrement: 'JIAN' })
  1. 【…xxx】:對象運算符

  2. mapMutations({ increment: 'JIA', decrement: 'JIAN' })】:就是下面代碼的簡寫形式

【再拆解一下】

  • increment----->increment() {}
  • ‘JIA’----> this.$store.commit(‘JIA’)
increment() {
    // 組件派發(fā)【dispatch】任務到actions,然后在actions中觸發(fā)mutations中的方法
    this.$store.commit('JIA');
},
decrement() {
    this.$store.commit('JIAN');
},
  1. 但是,我們發(fā)現(xiàn)commit中沒有傳數(shù)據(jù),所以我們看到頁面展示如下:

前端vue入門(純代碼)22_四個map,Vue前端,前端,vue.js,javascript

  1. 那么,我們?nèi)绾尾拍馨褦?shù)據(jù)傳遞過去呢?
  • 我們看一下官網(wǎng)給的例子:

  • methods: {
        ...mapMutations([
          'increment', 
      // 將 `this.increment()` 映射為 `this.$store.commit('increment')`
    
          // `mapMutations` 也支持載荷:
          'incrementBy' 
          //  將 `this.incrementBy(amount)` 映射為 
          // `this.$store.commit('incrementBy', amount)`
        ]),
    
  • 所以,可以這樣傳遞數(shù)據(jù):increment()---->increment(n)

    <button @click="increment(n)">+</button>
    
    methods:{
        //借助mapMutations生成對應的方法,方法中會調(diào)用commit去聯(lián)系mutations(對象寫法)
        ...mapMutations({ increment: 'JIA'}),
    }
    
  • 所以,映射后:increment(n) + { increment: 'JIA'}---映射成--->下面的代碼

    increment(n) {
        // 組件派發(fā)【dispatch】任務到actions,然后在actions中觸發(fā)mutations中的方法
        this.$store.commit('JIA',n);
    },
    
  • 最后,去mutations中調(diào)用字符串事件JIA。

前端vue入門(純代碼)22_四個map,Vue前端,前端,vue.js,javascript

2.mapActions 輔助函數(shù)

將 store 中的 actions 映射到局部方法。

  • mapActions方法:用于幫助我們在組件中生成與actions對話的方法,即:包含$store.dispatch(xxx)的函數(shù)

導入mapActions

//幫助我們將store中的actions部分,映射到本組件中使用。
import { mapActions } from 'vuex';

使用mapActions的組件中

methods:{
    //借助mapActions生成對應的方法,方法中會調(diào)用dispatch去聯(lián)系actions(對象寫法)
    ...mapActions({incrementOdd:'jiaOdd',incrementWait:'jiaWait'})

    //借助mapActions生成對應的方法,方法中會調(diào)用dispatch去聯(lián)系actions(數(shù)組寫法)
    // ...mapActions(['jiaOdd','jiaWait'])
}
  • 代碼我們就不解讀:和mapMutations一樣。

備注:mapActions與mapMutations使用時,若需要傳遞參數(shù)需要:在模板中綁定事件時傳遞好參數(shù),否則參數(shù)是事件對象。

vuex求和案例—mapActions和mapMutations版完整代碼:

Count.vue

<template>
	<div>
		<h1>當前求和為:{{ sum }}</h1>
		<h3 style="color: red">當前求和放大10倍為:{{ bigSum }}</h3>
		<h3>我叫{{ Sname }},是一名{{ Sjob }}</h3>
		<select v-model.number="n">
			<option value="1">1</option>
			<option value="2">2</option>
			<option value="3">3</option>
		</select>
		<button @click="increment(n)">+</button>
		<button @click="decrement(n)">-</button>
		<button @click="incrementOdd(n)">當前求和為奇數(shù)再加</button>
		<button @click="incrementWait(n)">等一等再加</button>
	</div>
</template>

<script>
import { mapGetters, mapState, mapMutations, mapActions } from 'vuex';
export default {
	name: 'Count',
	data() {
		return {
			n: 1,
		};
	},
	computed: {
		//#region

		//借助mapGetters生成計算屬性,從getters中讀取數(shù)據(jù)。(對象寫法)
		// ...mapGetters({bigSum:'bigSum'})

		//借助mapGetters生成計算屬性,從getters中讀取數(shù)據(jù)。(數(shù)組寫法)
		...mapGetters(['bigSum']),
		//#endregion

		//#region
		// 2.不借助mapState
		/*     sum(){
      return this.$store.state.sum
    },
    Sjob(){
      return this.$store.state.Sjob
    },
    Sname(){
      return this.$store.state.Sname
    }, */

		//借助mapState生成計算屬性,從state中讀取數(shù)據(jù)。(對象寫法)
		// ...mapState({sum:'sum',Sjob:'Sjob',Sname:'Sname'}),

		//借助mapState生成計算屬性,從state中讀取數(shù)據(jù)。(數(shù)組寫法)
		...mapState(['sum', 'Sjob', 'Sname']),
		//#endregion
	},
	methods: {
		//*************程序員親自寫方法******************
		/* increment() {
			// 組件派發(fā)【dispatch】任務到actions,然后在actions中觸發(fā)mutations中的方法
			this.$store.commit('JIA', this.n);
		},
		decrement() {
			this.$store.commit('JIAN', this.n);
		},*/

		//**********借助mapMutations方法****************
		//借助mapMutations生成對應的方法,方法中會調(diào)用commit去聯(lián)系mutations(對象寫法)
		...mapMutations({ increment: 'JIA', decrement: 'JIAN' }),
		//借助mapMutations生成對應的方法,方法中會調(diào)用commit去聯(lián)系mutations(數(shù)組寫法)
		// ...mapMutations([ 'JIA', 'JIAN' ]),

    //*************程序員親自寫方法******************
		/* incrementOdd() {
			this.$store.dispatch('jiaOdd', this.n);
		},
		incrementWait() {
			this.$store.dispatch('jiaWait', this.n);
		}, */

    //**********借助mapMutations方法****************
			//借助mapActions生成對應的方法,方法中會調(diào)用dispatch去聯(lián)系actions(對象寫法)
			...mapActions({incrementOdd:'jiaOdd',incrementWait:'jiaWait'})

			//借助mapActions生成對應的方法,方法中會調(diào)用dispatch去聯(lián)系actions(數(shù)組寫法)
			// ...mapActions(['jiaOdd','jiaWait'])
	},
	mounted() {
		console.log('Count', this.$store);
	},
};
</script>

<style scoped>
button {
	margin-left: 10px;
}
select {
	margin-left: 20px;
}
</style>

到了這里,關于前端vue入門(純代碼)22_四個map的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 前端vue入門(純代碼)09

    前端vue入門(純代碼)09

    【 09.vue中組件的自定義事件 】 自定義組件鏈接 在vue中用的click【點擊】、keyup【按鍵】……等事件,這些屬于內(nèi)置事件,也就是js自帶的事件。 問題一:什么是組件自定義事件呢? 【內(nèi)置事件】:是給html元素用的,比如span、div等標簽,不是組件。 【自定義事件】:顧名思義

    2024年02月09日
    瀏覽(17)
  • 前端vue入門(純代碼)35_導航守衛(wèi)

    前端vue入門(純代碼)35_導航守衛(wèi)

    星光不問趕路人,時光不負有心人 【 33.Vue Router--導航守衛(wèi) 】 導航守衛(wèi) 正如其名, vue-router 提供的導航守衛(wèi)主要用來通過跳轉或取消的方式守衛(wèi)導航。有多種機會植入路由導航過程中:全局的, 單個路由獨享的, 或者組件級的。 記住 參數(shù)或查詢的改變并不會觸發(fā)進入/離開的

    2024年02月16日
    瀏覽(19)
  • 前端vue入門(純代碼)24_Modules

    前端vue入門(純代碼)24_Modules

    窮不怪父,苦不責妻,方為真男人! 【 23.Vuex中的模塊化和命名空間 】 [可以去官網(wǎng)看看Vuex3文檔](Module | Vuex (vuejs.org)) 由于使用單一狀態(tài)樹,應用的所有狀態(tài)會集中到一個比較大的對象。當應用變得非常復雜時,store 對象就有可能變得相當臃腫。 為了解決以上問題,Vuex 允許

    2024年02月13日
    瀏覽(19)
  • 前端vue入門(純代碼)21_vuex

    前端vue入門(純代碼)21_vuex

    努力不一定成功,但是,不努力一定很輕松!??! 【 23.Vuex 】 [可以去官網(wǎng)看看Vuex3文檔](Vuex 是什么? | Vuex (vuejs.org)) 問題1:Vuex是什么? 【官方理解1】:Vuex 是一個專為 Vue.js 應用程序開發(fā)的 狀態(tài)【數(shù)據(jù)】管理模式 。它采用集中式存儲管理應用的所有組件的狀態(tài),并以相應

    2024年02月13日
    瀏覽(26)
  • vue-codemirror實現(xiàn)一個前端代碼在線編輯器,可處理 HTML,VUE,JS,CSS代碼在線編輯

    vue-codemirror實現(xiàn)一個前端代碼在線編輯器,可處理 HTML,VUE,JS,CSS代碼在線編輯

    先找個目錄創(chuàng)建一個vue項目 例如 我們想要項目叫 editor 在終端執(zhí)行 2和3其實都可以 但個人建議 最近還是2會更穩(wěn)定一些 在終端執(zhí)行 引入依賴包 然后在項目src目錄下創(chuàng)建 utils 文件夾 里面創(chuàng)建一個setting.js 參考代碼如下 然后 這邊 調(diào)用組件的代碼 因為項目也剛創(chuàng) 我直接寫 s

    2024年02月15日
    瀏覽(32)
  • 免費下載xlsx.full.min.js包,并放入前端代碼里,在html+vue中引用

    訪問 xlsx.js 的 GitHub 頁面:https://github.com/SheetJS/sheetjs 在 GitHub 頁面中,找到 \\\"Code\\\" 按鈕,然后點擊它,在下拉菜單中選擇 \\\"Download ZIP\\\",以下載 xlsx.js 的最新版本。 解壓下載的 ZIP 文件。 在解壓后的文件夾中,你會找到 xlsx.full.min.js 文件。將這個文件復制到你的前端項目中,通

    2024年02月02日
    瀏覽(63)
  • JavaScript - 判斷當前時間是否在指定區(qū)間內(nèi),例如:9:00~12:00(檢查當前時間是否處于規(guī)定的兩個時間段范圍內(nèi)),適用于 vue.js / uniapp / 微信小程序等前端項目

    例如,您想知道當前時間是否處于 9:00 ~ 12:00 時間區(qū)間內(nèi),然后根據(jù)這個判斷進而實現(xiàn)業(yè)務邏輯。 如下示例所示, 本文提供一個函數(shù),您只需要傳入 2 個時間區(qū)間,便可得出當前時間是否在該時間區(qū)間范圍內(nèi): 您可以一鍵復制,直接粘貼到您的項目中。 您只需要傳入開始時

    2024年02月16日
    瀏覽(50)
  • 〖大前端 - 基礎入門三大核心之JS篇?〗- JavaScript 的「數(shù)組」

    〖大前端 - 基礎入門三大核心之JS篇?〗- JavaScript 的「數(shù)組」

    當前子專欄 基礎入門三大核心篇 是免費開放階段 。 推薦他人訂閱,可獲取扣除平臺費用后的35%收益,文末名片加V! 說明:該文屬于 大前端全棧架構白寶書專欄, 目前階段免費開放 , 購買任意白寶書體系化專欄可加入 TFS-CLUB 私域社區(qū)。 福利:除了通過訂閱\\\"白寶書系列專

    2024年02月04日
    瀏覽(23)
  • [前端系列第3彈]JS入門教程:從零開始學習JavaScript

    本文將帶領大家,從零開始學習JavaScript,fighting~ 目錄 一、JavaScript簡介 二、變量和數(shù)據(jù)類型 三、注釋和分號 四、算術運算符 五、表達式和語句 六、代碼塊和作用域 七、函數(shù)(最重要)? ????????JavaScript(簡稱JS)是一種運行在瀏覽器中的腳本語言,它可以讓網(wǎng)頁變得

    2024年02月13日
    瀏覽(95)
  • ArcGIS Maps SDK for JavaScript系列之一:在Vue3中加載ArcGIS地圖

    ArcGIS Maps SDK for JavaScript系列之一:在Vue3中加載ArcGIS地圖

    ArcGIS Maps SDK for JavaScript 是由 Esri 公司開發(fā)的一款用于構建交互式地圖應用程序的 JavaScript 庫。它提供了豐富的地圖顯示、分析和可視化功能,適用于各種場景。 目前,ArcGIS Maps SDK for JavaScript 提供兩個主要版本:3.x 和 4.x。 ArcGIS Maps SDK for JavaScript 3.x 版本: 3.x 版本是 ArcGIS

    2024年02月13日
    瀏覽(96)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包