vue?當(dāng)中組件之間共享數(shù)據(jù)的實(shí)現(xiàn)方式
vue組件之間共享數(shù)據(jù)方式
- 父向子傳值:v-bind 屬性綁定
- 子向父?jìng)髦担簐-on 事件綁定
- 兄弟組件之間共享數(shù)據(jù):EventHBus
$emit 發(fā)送數(shù)據(jù)的那個(gè)組件
$on 接收數(shù)據(jù)的那個(gè)組件
上面介紹的這三種組件之間的通訊方式,只適合在小范圍內(nèi)來(lái)進(jìn)行數(shù)據(jù)的共享,如果我們要頻繁的、大范圍的實(shí)現(xiàn)數(shù)據(jù)的共享,以上三種方式就有點(diǎn)力不從心了
Vuex
vuex是實(shí)現(xiàn)組件全局狀態(tài)(數(shù)據(jù))管理的一種機(jī)制,可以方便的實(shí)現(xiàn)組件之間的數(shù)據(jù)共享
使用vuex統(tǒng)一管理狀態(tài)的好處
- 能夠在 vuex 中集中管理共享的數(shù)據(jù),易于開(kāi)發(fā)和維護(hù)
- 能夠高效的實(shí)現(xiàn)組件之間的數(shù)據(jù)共享,提高開(kāi)發(fā)效率
- 存儲(chǔ)在 vuex 中的數(shù)據(jù)都是響應(yīng)式的,能夠?qū)崟r(shí)保持?jǐn)?shù)據(jù)與頁(yè)面的同步
一般情況下,只有組件之間共享的數(shù)據(jù),才有必要存儲(chǔ)到 vuex 中,對(duì)于組件中的私有數(shù)據(jù),依舊存儲(chǔ)在組件自身的data中即可
vuex 的基本使用
1. 安裝 vuex 依賴包
npm install vuex --save
2. 導(dǎo)入 vuex 包
import Vuex from 'vuex' Vue.use(vuex)
3. 創(chuàng)建store對(duì)象
const store = new Vuex.Store({
? ? // state 中存放的就是全局共享的數(shù)據(jù)
? ? state: { count: 0 }
})4. 將 store 對(duì)象掛載到 vue 實(shí)例中
new Vue({
? ? el: '#app',
? ? render: h => h(app),
? ? router,
? ? // 將創(chuàng)建的共享數(shù)據(jù)對(duì)象,掛載到 vue 實(shí)例中
? ? // 所有的組件,就可以直接從 store 中獲取全局的數(shù)據(jù)了
? ? store
})vuex 中的主要核心概念
state
State提供唯一的公共數(shù)據(jù)源,所有共享的數(shù)據(jù)都要統(tǒng)一放到 Store 的 State 中進(jìn)行存儲(chǔ)
// 創(chuàng)建store數(shù)據(jù)源,提供唯一的公共數(shù)據(jù)
const store = new Vuex.Store({
? ? state: { count:0 }
})組件訪問(wèn)state中數(shù)據(jù)的第一種方式:
this.$store.state.全局?jǐn)?shù)據(jù)名稱
組件訪問(wèn)state中數(shù)據(jù)的第二種方式:
// 1. 從 vuex 中按需導(dǎo)入 mapState 函數(shù)
import { mapState } from 'vuex'通過(guò)剛才導(dǎo)入到 mapState 函數(shù),將當(dāng)前組件需要的全局?jǐn)?shù)據(jù),映射為當(dāng)前組件的 computed 計(jì)算屬性:
// 2. 將全局?jǐn)?shù)據(jù),映射為當(dāng)前組件的計(jì)算屬性
computed: {
? ? ...mapState(['count'])
}Mutation
Mutation 用于變更 Store 中的數(shù)據(jù)
只能通過(guò) Mutation 變更 Store 中的數(shù)據(jù),不可以直接操作 Store 中的數(shù)據(jù)
通過(guò)這種方式雖然操作起來(lái)稍微繁瑣一些,但是可以集中監(jiān)控所有數(shù)據(jù)的變化
// 定義 Mutation
const store = new Vuex.Store({
? ? state:{
? ? ? ? count: 0
? ? }
}),
mutations:{
? ? add(state){
? ? ? ? //變更狀態(tài)
? ? ? ? state.count++
? ? }
}
?
// 觸發(fā)mutation
methods:{
? ? handle(){
? ? ? ? //觸發(fā) mutation 的第一種方式
? ? ? ? this.$store.commit('add')
? ? }
}在觸發(fā) Mutation 時(shí)傳遞參數(shù):
// 定義 Mutation
const store = new Vuex.Store({
? ? state:{
? ? ? ? count: 0
? ? },
? ? mutations:{
? ? ? ? addN(state,step){
? ? ? ? ? ? // 變更狀態(tài)
? ? ? ? ? ? state.count += step
? ? ? ? }
? ? }
})
?
?
// 觸發(fā)mutation
methods:{
? ? handle2(){
? ? ? ? // 在調(diào)用 commit 函數(shù),觸發(fā) mutations 是攜帶參數(shù)
? ? ? ? this.$store.commit('addN',6)
? ? }
}this.$store.commit() 是觸發(fā) mutations 的第一種方式,觸發(fā) mutations 的第二種方式:
//1. 從 vuex 中按需導(dǎo)入 mapMutations 函數(shù)
import { mapMutations } from 'vuex'通過(guò)剛才導(dǎo)入的 mapMutations 函數(shù),將需要的 mutations 函數(shù),映射為當(dāng)前組件的 methods 方法:
methods:{
? ? ...mapMutations(['add','addN']),
? ? // 下面是在組件中的使用方法,此時(shí)映射的 add 和 addN 函數(shù)就相當(dāng)于當(dāng)前組件的方法,通過(guò)this調(diào)用即可
? ? btnHandle1(){
? ? ? ? this.add()
? ? }
? ? btnHandle2(){
? ? ? ? this.addN()
? ? }
}Action
Action用來(lái)處理異步任務(wù)。
如果通過(guò)異步操作變更數(shù)據(jù),必須通過(guò) Action,而不能使用 Mutation ,但是 Action 中還是要通過(guò)觸發(fā) Mutation 的方式間接變更數(shù)據(jù)
觸發(fā) actions 的第一種方式是 通過(guò) this.$store.dispatch('函數(shù)名稱')
觸發(fā) actions 的第二種方式是 通過(guò) 按需導(dǎo)入 mapActions函數(shù),通過(guò)導(dǎo)入的 mapActions 函數(shù),將需要的 actions 函數(shù),映射為當(dāng)前組件的 methods 方法即可
// 定義 Action
const store = new Vuex.Store({
? ? state:{
? ? ? ? count: 0
? ? },
? ? mutations:{
? ? ? ? add(state){
? ? ? ? ? ? state.count++
? ? ? ? },
? ? ? ? //攜帶參數(shù)的形式
? ? ? ? addN(state,step){
? ? ? ? ? ? state.count += step
? ? ? ? }
? ? },
? ? actions:{
? ? ? ? addAsync(context){
? ? ? ? ? ? setTimeout(()=>{
? ? ? ? ? ? ? ? context.commit('add')
? ? ? ? ? ? },1000)?
? ? ? ? },
? ? ? ? // actions 攜帶參數(shù)的形式
? ? ? ? addAsyncN(context,step){
? ? ? ? ? ? setTimeout(()=>{
? ? ? ? ? ? ? ? context.commit('addN',step)
? ? ? ? ? ? },1000)
? ? ? ? }
? ? }
})
?
?
// 觸發(fā) Action
methods:{
? ? handle(){
? ? ? ? // 觸發(fā) actions 的第一種方式
? ? ? ? this.$store.dispatch('addAsync')
? ? },
? ? handle2(){
? ? ? ? // 觸發(fā) actions 時(shí)攜帶參數(shù)
? ? ? ? this.$store.dispatch('addAsyncN',6 )
? ? }
}Getter
- Getter 用于對(duì) Store 中的數(shù)據(jù)進(jìn)行加工處理形成新的數(shù)據(jù)
- Getter 可以對(duì) Store 中已有的數(shù)據(jù)加工處理之后形成新的數(shù)據(jù),類似 Vue 的計(jì)算屬性
Store中的數(shù)據(jù)發(fā)生變化時(shí),Getter的數(shù)據(jù)也會(huì)跟著發(fā)生變化
// 定義 getter
const store = new Vuex.Store({
? ? state:{
? ? ? ? count: 0
? ? },
? ? mutations:{},
? ? Actions:{},
? ? getters:{
? ? ? ? showNum: state => {
? ? ? ? ? ? return '當(dāng)前最新的數(shù)量是【'+ state.count +'】'
? ? ? ? }
? ? }
})使用 getters 的第一種方式:
this.$store.getters.名稱
使用 getters 的第二種方式:
import { mapGetters } from 'vuex'
computed:{
? ? ...mapGetters(['showNum'])
}以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Vue3學(xué)習(xí)筆記之依賴注入Provide/Inject
- vue中的依賴注入provide和inject簡(jiǎn)單介紹
- Vue?privide?和inject?依賴注入的使用詳解
- Vue?2源碼閱讀?Provide?Inject?依賴注入詳解
- Vue 2.0 中依賴注入 provide/inject組合實(shí)戰(zhàn)
- vue+vuex+axios從后臺(tái)獲取數(shù)據(jù)存入vuex,組件之間共享數(shù)據(jù)操作
- vue+vuex+axio從后臺(tái)獲取數(shù)據(jù)存入vuex實(shí)現(xiàn)組件之間共享數(shù)據(jù)
- Vue 使用依賴注入的方式共享數(shù)據(jù)的過(guò)程
相關(guān)文章
VsCode工具開(kāi)發(fā)vue項(xiàng)目必裝插件清單(推薦!)
對(duì)于很多使用vscode編寫(xiě)vue項(xiàng)目的新手同學(xué)來(lái)說(shuō),可能不知道使用什么插件,下面這篇文章主要給大家介紹了關(guān)于VsCode工具開(kāi)發(fā)vue項(xiàng)目必裝插件的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
vue-router 控制路由權(quán)限的實(shí)現(xiàn)
這篇文章主要介紹了vue-router 控制路由權(quán)限的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
vue實(shí)現(xiàn)富文本編輯器詳細(xì)過(guò)程
Vue富文本的實(shí)現(xiàn)可以使用一些現(xiàn)成的第三方庫(kù),如Quill、Vue-quill-editor、wangEditor等,這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)富文本編輯器的相關(guān)資料,需要的朋友可以參考下2024-01-01
在vue項(xiàng)目中使用Nprogress.js進(jìn)度條的方法
NProgress.js是輕量級(jí)的進(jìn)度條組件,使用簡(jiǎn)便,可以很方便集成到單頁(yè)面應(yīng)用中。這篇文章主要介紹了在vue項(xiàng)目中使用Nprogress.js進(jìn)度條的方法,需要的朋友可以參考下2018-01-01
vue項(xiàng)目中使用crypto-js實(shí)現(xiàn)加密解密方式
這篇文章主要介紹了vue項(xiàng)目中使用crypto-js實(shí)現(xiàn)加密解密方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Vue關(guān)于數(shù)據(jù)綁定出錯(cuò)解決辦法
這篇文章主要介紹了Vue關(guān)于數(shù)據(jù)綁定出錯(cuò)解決辦法的相關(guān)資料,需要的朋友可以參考下2017-05-05
詳解Vue串聯(lián)過(guò)濾器的使用場(chǎng)景
這篇文章主要介紹了詳解Vue串聯(lián)過(guò)濾器的使用場(chǎng)景,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04

