成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

一文讓你徹底搞懂Vuex,滿滿的干貨

開發(fā) 前端
可以把多個組件都需要的變量全部存儲到一個對象里面,然后這個對象放在頂層的 vue 實例中,讓其他組件可以使用。這樣多個組件就可以共享這個對象中的所有屬性。

[[429955]]

官方解釋:Vuex 是專為 vue.js 應用程序開發(fā)的狀態(tài)管理模式。

一、Vuex 是做什么呢?

什么是狀態(tài)管理?

簡單地講:可以把多個組件都需要的變量全部存儲到一個對象里面,然后這個對象放在頂層的 vue 實例中,讓其他組件可以使用。這樣多個組件就可以共享這個對象中的所有屬性。

有些同學想著,這么簡單我們自己在vue頂層定義一個對象不就可以實現(xiàn)共享了?我們發(fā)現(xiàn)雖然數(shù)據(jù)可以獲取到,但是如果在某個組件內,數(shù)據(jù)改變了,那我們如何修改數(shù)據(jù),讓此數(shù)據(jù)在其他組件內保持最新呢?

我們的vuex就是為了提供一個在多個組件間共享狀態(tài)的插件,而且能夠實現(xiàn)實時響應。

二、Vuex 使用

vuex 是管理組件之間通信的一個插件,所以使用之前必須安裝。

2.1、安裝

1)使用 script 方式引入

  1. <script src="https://cdn.bootcss.com/vuex/3.0.1/vuex.min.js"></script> 

2)使用包管理

  1. npm install vuex --save //yarn add vuex 

注意:vuex 必須依賴 vue 使用

2.2、搭建 store 實例

創(chuàng)建一個 store 文件夾,新建 index.js

  1. import Vue from "vue"
  2. import Vuex from "vuex"
  3.  
  4. Vue.use(Vuex);//使用vuex 
  5.  
  6. export default new Vuex.Store({ 
  7.     state:{}, 
  8.   mutations:{}, 
  9.   getters:{}, 
  10.   actions:{}, 
  11.   modules:{} 
  12. }) 

 在 main.js 處,掛載 store

  1. import store from './store' 
  2.  
  3. new Vue({ 
  4.  router, 
  5.  render: h=>h(App) 
  6. }) 
  7.  
  8. //相當于 
  9. // Vue.prototype.$store = store 

 2.3、使用狀態(tài)

  1. // store 中定義狀態(tài) 
  2. state:{ 
  3.     statue: '在線' 
  4.  
  5. //在組件內使用 
  6. <div>組件內數(shù)據(jù):{{ $store.state.status }} </div> 
  7. //在 js 中使用 
  8. mounted(){ 
  9.     console.log( this.$store.state.status ) 

三、Vuex 的五大核心

3.1、state

state 存放 vuex 的基本數(shù)據(jù),用來存儲變量的。

單一狀態(tài)樹

Vuex 使用單一狀態(tài)樹,即用一個對象就包含了全部的狀態(tài)數(shù)據(jù)。state 作為構造器選項,定義了所有我們需要的基本狀態(tài)參數(shù)。

在組件中引用 state 數(shù)據(jù)方式:

1)通過 vue 的 computed 獲取 vuex 的 state

  1. export default new Vuex.Store({ 
  2.  state:{ 
  3.   online:true 
  4.  } 
  5. }) 
  6.  
  7.  
  8. computed:{ 
  9.  status(){ 
  10.   return this.$store.state.online 
  11.  } 

store 中的數(shù)據(jù)發(fā)生改變時,都會重新求取計算屬性,并更新相關 DOM。

2)如果需要使用多個參數(shù)時,都使用 computed 計算屬性,就會使代碼變的有些冗余和復雜,此時我們就可以借助 mapState 輔助函數(shù)。

  1. //state 中數(shù)據(jù)比較多,引用到一個組件內 
  2. export default new Vuex.Store({ 
  3.  state:{ 
  4.   online:true
  5.    per:[ 
  6.     {name:'qq',age:18} 
  7.    ], 
  8.   role:'管理員' 
  9.  } 
  10. }) 
  11.  
  12. //組件內 
  13. import { mapState } from 'vuex' 
  14.  
  15. export default { 
  16.   name'App'
  17.   computed: mapState({ 
  18.    online: state => state.online, 
  19.     per: 'per', // 'per'就相當于 state.per 
  20.     role: 'role' // 'role'就相當于 state.role 
  21.   }) 

vuex 使用單一狀態(tài)樹來管理應用層級的全部狀態(tài),單一狀態(tài)樹能夠讓我們最直接的方式找到某個狀態(tài)的片段,而且之后的維護和調試過程,也可以非常方便管理和維護。

3.2、getters

從 store 中獲取一些 state 變異后的狀態(tài)。

使用的時候一般有兩種方式:

1)返回的結果只依賴于 state 中的數(shù)據(jù)

  1. export default new Vuex.Store({ 
  2.  state:{ 
  3.   count:2, 
  4.  }, 
  5.  getters:{ 
  6.    //返回 count 的 2 倍數(shù)據(jù) 
  7.   countDouble(state){ 
  8.    return state.count*2 
  9.   } 
  10.  } 
  11. }) 
  12.  
  13. //組件中引用 
  14. <div> 獲取countDouble:{{ $store.getters.countDouble }} </div> 
  15. //運行結果 
  16. 獲取countDouble:4 

此處,$store.getters.countDouble 的使用與上邊的 $store.state.count 是一樣的。

2)getters 中返回的變異結果,依賴于某個 getters 中屬性返回結果

  1. export default new Vuex.Store({ 
  2.  state:{ 
  3.   count:2, 
  4.  }, 
  5.  getters:{ 
  6.    //返回 count 的 2 倍數(shù)據(jù) 
  7.   countDouble( state ){ 
  8.    return state.count * 2 
  9.   } 
  10.    //返回 countDouble 的 2 倍數(shù)據(jù) 
  11.    countDoubleT( state , getters ){ 
  12.     return getters.countDouble * 2 
  13.    } 
  14.  } 
  15. }) 
  16.  
  17. //組件中引用 
  18. <div> 獲取countDouble:{{ $store.getters.countDoubleT }} </div> 
  19. //運行結果 
  20. 獲取countDouble:8 

3.3、mutations

vuex 的store 狀態(tài)的更新唯一方式:提交 Mutation。

Mutations 主要包括兩部分:

字符串的事件類型

一個回調函數(shù),該回調函數(shù)的第一個參數(shù)就是 state。

1)mutation 中的方法通過 commit 調用,不傳參數(shù)使用:

  1. export default new Vuex.Store({ 
  2.  state:{ 
  3.   count:2, 
  4.  }, 
  5.  mutations:{ 
  6.   incrs(state){ 
  7.    // count 加 1  
  8.    state.count++ 
  9.   } 
  10.  } 
  11. }) 
  12.  
  13. //組件調用 
  14.  
  15. <button @click=" $store.commit('incrs') " >+</button> 
  16. {{$store.state.count}} 

按鈕每點一次,count 就會自加一次。

2)mutations 傳遞參數(shù)

我們點擊加按鈕時,指定每次點擊增加的數(shù)值,如下:

  1. export default new Vuex.Store({ 
  2.  state:{ 
  3.   count:2, 
  4.  }, 
  5.  mutations:{ 
  6.   addNum( state,n ){ 
  7.    // count 加 1  
  8.    state.count +=n 
  9.   } 
  10.  } 
  11. }) 
  12.  
  13. //組件調用 
  14.  
  15. <button @click=" $store.addNum( 'incrs' , 5 ) " >+</button> 
  16. {{$store.state.count}} 
  17.  
  18. //運行結果 
  19. 每點一次按鈕,count 增加 5 

上個實例傳遞的是一個參數(shù),如果我們需要傳遞多個參數(shù)時,該如何實現(xiàn)呢?

3)mutations 傳遞多個參數(shù)

  1. export default new Vuex.Store({ 
  2.  state:{ 
  3.   count:2, 
  4.  }, 
  5.  mutations:{ 
  6.   addNum(state,payload){ 
  7.     // payload 是傳遞過來的參數(shù)對象 
  8.     state.count += payload.count 
  9.   } 
  10.  } 
  11. }) 
  12.  
  13. //組件調用 
  14.  
  15. <button @click="addTen" >加10</button> 
  16. {{$store.state.count}} 
  17. export default
  18. methods:{ 
  19.  addTen(){ 
  20.   this.$store.commit({ 
  21.     type:'addNum'
  22.      count:10, 
  23.         ...//可以傳任意多個參數(shù) 
  24.     }) 
  25.   } 
  26.  } 
  27. //運行結果 
  28. 每點一次按鈕,count 增加 10 

上述方法是 mutations 特殊的提交封裝。包含了 type 屬性的對象,將整個 commit 的對象作為 payload 使用。

3.4、actions

mutations 提交更新數(shù)據(jù)的方法,必須是同步的,如果是異步使用就會出現(xiàn)問題,然后在項目開發(fā)中往往就會用到異步更新,比如網(wǎng)路請求數(shù)據(jù)。

actions 是類似于 mutation,功能大致相同,但是 actions 是用來替代 mutations 進行異步操作的。

1)actions 的基本使用

  1. actions:{ 
  2.  aUpdateCount(context){ 
  3.   setTimeout(()=>{ //使用定時器模擬異步操作 
  4.    context.commit('updateCount'
  5.   },2000) 
  6.  } 
  7. }, 
  8. mutations:{ 
  9.  updateCount(state){ 
  10.   state.count = 5201314 
  11.  }, 
  12.  
  13. // 組件內使用 
  14. {{$store.state.count}} 
  15. <button @click="$store.dispatch('aUpdateCount')">異步更新count</button> 
  16.  
  17. //運行結果 
  18. 點擊按鈕,兩秒后更新 count 值為5201314 

值得注意的是,使用 actions 異步更新數(shù)據(jù)的時候,還是需要經(jīng)過 mutations 中的方法,state 中的數(shù)據(jù)只能由 mutations 中的方法修改。

調用 mutations 中的方法,使用 commit 調用。

調用 actions 中的方法,使用 dispatch 調用。

2)異步更新的時候,也可以帶參數(shù)

  1. // 功能:點擊按鈕,指定 count 修改的值 
  2. actions:{ 
  3.  aUpdateCount( context, payload ){ 
  4.   setTimeout(()=>{ //使用定時器模擬異步操作 
  5.    context.commit'updateCount' , payload ) 
  6.   },2000) 
  7.  } 
  8. }, 
  9. mutations:{ 
  10.  updateCount( state , payload ){ 
  11.   state.count = payload 
  12.  }, 
  13.  
  14. // 組件內使用 
  15. {{$store.state.count}} 
  16. <button @click="$store.dispatch( 'aUpdateCount', '我愛前端' )">異步更新count</button> 
  17.  
  18. //運行結果 
  19. 點擊按鈕,兩秒后更新 count 值為: 我愛前端 

3)傳入異步參數(shù)

  1. actions:{ 
  2.  //傳入promise 
  3.  updateData(context,payload){ 
  4.   return new Promise((resolve,reject)=>{ 
  5.    setTimeout(()=>{ 
  6.     resolve('我學會了'
  7.    },2000) 
  8.   }) 
  9.  }, 
  10.  
  11. //組件內調用 
  12. <button @click="ok">promise執(zhí)行成功,返回"我學會了"</button> 
  13. methods:{ 
  14.  ok(){ 
  15.   this.$store.dispatch('updateData').then((res)=>{ 
  16.    console.log(res) 
  17.   }) 
  18.  }, 
  19.  
  20. //運行結果 
  21. 點擊按鈕,兩秒后打印:我學會了 

 3.5、modules

modules 是模塊的意思,vue 使用單一狀態(tài)樹,項目越來越大,store 中的數(shù)據(jù)越來越多,不便于數(shù)據(jù)的管理和維護,代碼也會變得臃腫。因此使用 modules ,把數(shù)據(jù)劃分到對應的某個模塊,既便于開發(fā),也提高代碼的可讀性。

1)modules 簡單使用

  1. import Vue from 'vue' 
  2. import Vuex from 'vuex' 
  3. import { Increase } from './mutation_type.js' 
  4. Vue.use(Vuex) 
  5. export default new Vuex.Store({ 
  6.   state: {}, 
  7.   actions: {}, 
  8.   getters: { }, 
  9.   mutations: { }, 
  10.   modules:{ 
  11.     a:{ 
  12.       state:{}, 
  13.       getters:{}, 
  14.       mutations:{}, 
  15.       actions:{} 
  16.     }, 
  17.     b:{ 
  18.       state:{}, 
  19.       getters:{}, 
  20.       mutations:{}, 
  21.       actions:{} 
  22.     } 
  23.   }, 
  24. }) 
  25.  
  26. //也可以整理為 
  27. const moduleA = { 
  28.   state:{}, 
  29.   getters:{}, 
  30.   mutations:{}, 
  31.   actions:{} 
  32. const moduleB = { 
  33.   state:{}, 
  34.   getters:{}, 
  35.   mutations:{}, 
  36.   actions:{} 
  37. Vue.use(Vuex) 
  38. export default new Vuex.Store({ 
  39.   state: {}, 
  40.   actions: {}, 
  41.   getters: { }, 
  42.   mutations: { }, 
  43.   modules:{ 
  44.     a: moduleA, 
  45.     b: moduleB 
  46.   }, 
  47. }) 

 2)模塊中的數(shù)據(jù)如何使用呢?

  1. const moduleA = { 
  2.   state:{ 
  3.     aName:'我是模塊a的數(shù)據(jù)' 
  4.   }, 
  5.   getters:{}, 
  6.   mutations:{}, 
  7.   actions:{} 
  8.  
  9. // 組件內引用 
  10. {{ $store.state.a.aName }} 

 3)調用模塊內的 mutations 中的方法,如何調用呢?

  1. $store.commit('aChangeName'

調取模塊內的 mutations 中的方法,與之前是一模一樣的,程序會先從第一層 store 中查找方法,找不到方法時會繼續(xù)去模塊中查找。

4)調用模塊內的 getters 內方法

  1. $store.getters.getName 

需要注意的是,getters 中方法都是對 state 中數(shù)據(jù)變異,如果模塊的 getters 方法需要根 store 中的 state 呢?

  1. getName(state,getters , rootState){ 
  2.   // state 表示當前模塊的 state 
  3.   // getters表示當前模塊的getters 
  4.   //rootState 表示根 store 內的state 

5)模塊內的 actions 中的方法,使用 commit 調用 mutations 中方法時,只能調用本模塊內的 mutations 方法,不能調用外層的。

四、Vuex 數(shù)據(jù)響應原理

Vuex 的 store 中的 state 是響應式的,當 state 中數(shù)據(jù)發(fā)生改變時,vue 組件會自動更新。這就要求我們必須遵守一些vuex對應的規(guī)則:

提前在 store 中初始化好所需的屬性。

說人話,就是必須在state中定義的屬性才能做到響應式,如果是后加或刪除的,無法做到響應式。

舉個栗子:

  1. mutations:{ 
  2.  changeName(state){ 
  3.   state.info.name = '愛學習的前端人' 
  4.  }, 
  5.  addAdrs(state){ 
  6.   state.info['address'] = "陜西西安" 
  7.  }, 
  8.        
  9. {{this.$store.state.info}} 
  10. <button @click="$store.commit('changeName')">修改姓名</button> 
  11. <button @click="$store.commit('addAdrs')">增加地址</button>      

此時點擊修改姓名的時候,可以做到響應式,而點擊“增加地址”按鈕時,頁面沒有任何反應,但是在開發(fā)者模式中可以看到數(shù)據(jù)有變化。

我們要響應式,該如何實現(xiàn)呢?

  1. addAdrs(state){ 
  2.   
  3.   state.info['address'] = "陜西西安" 
  4.    
  5.   //修改為: 
  6.   Vue.set(state.info,'address','陜西西安')   
  7.    
  8.  }, 

 同樣的如果要刪除 age 屬性時,使用 delete 也做不到響應式,需要修改為 Vue.delete。

實例:響應式刪除 age 屬性

  1. deleteAge(state){ 
  2.    
  3.  //delete state.info.age 
  4.    
  5.  //修改為   
  6.  Vue.delete(state.info,'age'
  7. }, 
  8.   
  9. //組件內容 
  10. {{this.$store.state.info}} 
  11. <button @click="$store.commit('deleteAge')">刪除年齡</button> 

五、類型常量

在 mutation 中定義很多事件類型,也就是方法名。當項目越來越大時,Vuex 管理的狀態(tài)越來越多,需要更新狀態(tài)的情況越來越多,那么意為著 Mutations 中的方法名越來越多,方法過多時,使用的時候需要花費大量精力去記住或來回切換文件找方法名,這樣很容易出錯,所以推薦大家使用一個常量,即使方法名出錯了,也會將錯就錯,程序并不會發(fā)生異常。

如:

  1. // 新建 mutation_type.js 文件 
  2. //導出一個常量 
  3. export const Increase = 'increase' 
  4.  
  5. // store.js文件 
  6. import Vue from 'vue' 
  7. import Vuex from 'vuex' 
  8. import { Increase } from './mutation_type.js' 
  9. Vue.use(Vuex) 
  10. export default new Vuex.Store({ 
  11.   state:{ 
  12.     count:2, 
  13.   }, 
  14.   mutations:{ 
  15.     [Increase](state){ 
  16.       state.count++ 
  17.     }, 
  18.   } 
  19. }) 
  20.  
  21. //組件內容 
  22. {{ $store.state.count }} 
  23. <button @click="add">加10</button>  
  24. import { Increase } from './store/mutation_type' 
  25. sxport default
  26.     methods:{ 
  27.    add(){ 
  28.     this.$store.commit(Increase) 
  29.    } 
  30.   } 

使用的時候,只需要記住 Increase 或者在 mutation_type.js 文件內查找就好了。

責任編輯:姜華 來源: 今日頭條
相關推薦

2025-06-04 03:21:00

RAGRetrievalGeneratio

2021-06-30 08:45:02

內存管理面試

2020-03-18 14:00:47

MySQL分區(qū)數(shù)據(jù)庫

2022-06-07 10:13:22

前端沙箱對象

2024-09-26 07:27:27

2020-12-07 06:19:50

監(jiān)控前端用戶

2021-07-08 10:08:03

DvaJS前端Dva

2019-11-06 17:30:57

cookiesessionWeb

2024-08-08 14:57:32

2022-08-19 09:24:46

計算機技術

2022-04-11 10:56:43

線程安全

2021-08-05 06:54:05

觀察者訂閱設計

2023-04-12 08:38:44

函數(shù)參數(shù)Context

2023-11-23 06:50:08

括號

2020-12-18 09:36:01

JSONP跨域面試官

2022-09-29 10:26:59

iOSScaffoldflutter

2024-04-12 12:19:08

語言模型AI

2021-01-22 06:35:44

IoCxml驅動技術

2024-09-04 16:19:06

語言模型統(tǒng)計語言模型

2025-05-22 06:23:48

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲一区二区三区在线免费 | 欧美一区二区三区在线观看视频 | 日韩精品 电影一区 亚洲 | 国产伦精品一区二区三区精品视频 | 欧美视频成人 | 精品综合网 | 亚洲视频在线观看 | 日日骑| 亚洲欧美一区二区三区国产精品 | av网站免费观看 | 久久久91精品国产一区二区三区 | 久久久久久久一区 | 91国在线高清视频 | 四虎影视| 精品一区二区三区免费视频 | 日韩国产在线 | 97色伦网| 亚洲高清视频在线观看 | 欧美黄色大片在线观看 | 四虎影院在线观看免费视频 | 亚洲精品综合 | 国产ts人妖系列高潮 | 精品影院 | 亚洲国产aⅴ精品 | av免费网址 | 玖玖操| 成年人免费看 | 欧美成人免费 | 玖玖爱365| 在线中文视频 | 91久久久久久久久久久 | 久久亚洲国产精品日日av夜夜 | 午夜精品网站 | 精品久久影院 | 91在线视频免费观看 | 97精品超碰一区二区三区 | 午夜日韩视频 | 尤物在线精品视频 | 国产精品精品视频一区二区三区 | 国产特级毛片 | 日本一本在线 |