自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Vuex4 極速入門到上手

開發(fā) 前端
vuex4 是 vue3的兼容版本,提供了和vuex3 的相同API。因此我們可以在 vue3 中復(fù)用之前已存在的 vuex 代碼。

[[434975]]

vuex4 是 vue3的兼容版本,提供了和vuex3 的相同API。因此我們可以在 vue3 中復(fù)用之前已存在的 vuex 代碼。

一、安裝以及初始化

vuex4安裝:

  1. npm install vuex@next 

為了向vue3初始化方式看齊,vuex4 初始化方式做了相應(yīng)的變化,使用新的 createStore 函數(shù)創(chuàng)建新的 store 實(shí)例。

  1. import { createApp } from 'vue' 
  2. import App from './App.vue' 
  3. import router from './router' 
  4. import { createStore } from "vuex" 
  5.  
  6. const store = createStore({ 
  7.  state(){ 
  8.   return
  9.    num:1, 
  10.   } 
  11.  } 
  12. }) 
  13.  
  14. const app = createApp(App) 
  15. app.use(router) 
  16. app.use(store) 
  17. app.mount('#app'
  18.  
  19. //在組件內(nèi)使用時(shí)與之前一樣 
  20. <div>{{$store.state.num}}</div> 

二、vuex4在組件內(nèi)的使用

2.1、使用場(chǎng)景1

在組件的模板中直接使用,與之前的api保持一致

  1. // 在 main.js 內(nèi) 
  2. const store = createStore({ 
  3.  state(){ 
  4.   return
  5.    num:1, 
  6.   } 
  7.  }, 
  8.  mutations:{ 
  9.   addNum(state){ 
  10.    state.num++ 
  11.   } 
  12.  }, 
  13.  actions:{}, 
  14.  modules:{} 
  15. }) 
  16.  
  17. //組件內(nèi) 
  18. <div> 
  19.  {{$store.state.num}} 
  20.  <button @click="$store.commit('addNum')">num自加</button>    
  21. </div> 

2.2、使用場(chǎng)景2

通過(guò) useStore 把store 引入組件內(nèi),然后操作 store 。

  1. <template> 
  2.  <div> 
  3.   store組件 
  4.   {{state.num}} 
  5.   <button @click="add">num自加</button>  
  6.  </div> 
  7. </template> 
  8.  
  9. <script> 
  10. import { useStore } from "vuex" 
  11. export default { 
  12.  setup(){ 
  13.   const store = useStore() 
  14.   return
  15.    state:store.state, 
  16.    add(){ 
  17.     store.commit('addNum'
  18.    } 
  19.   } 
  20.  } 
  21. </script> 

2.3、使用場(chǎng)景3

store 內(nèi)使用到多個(gè)值時(shí),可以通過(guò) toRefs 方法,將 store.state 內(nèi)的數(shù)據(jù)直接展開。

  1. <template> 
  2.  <div> 
  3.   {{num}} 
  4.   <button @click="add">num自加</button> 
  5.  </div> 
  6. </template> 
  7.  
  8. <script> 
  9. import { useStore } from 'vuex' 
  10. import { toRefs } from "vue" 
  11. export default { 
  12.  setup(){ 
  13.   const store = useStore() 
  14.   return
  15.    ...toRefs(store.state), 
  16.    add(){ 
  17.     store.commit('addNum'
  18.    } 
  19.   } 
  20.  } 
  21. </script> 

三、 getters 的用法

與之前的用法保持一致:

  1. const store = createStore({ 
  2.  state(){ 
  3.   return
  4.    num:1, 
  5.   } 
  6.  }, 
  7.  getters:{ 
  8.   doubleNum(state){ 
  9.    return state.num*2 
  10.   } 
  11.  }, 
  12. }) 
  13.  
  14. //使用1:直接在template中使用 
  15. <template> 
  16.  {{$store.getters.doubleNum}} 
  17. </template> 
  18.  
  19. //使用2:利用計(jì)算屬性獲取 
  20. <template> 
  21.  <div> 
  22.   {{getDouble}} 
  23.  </div> 
  24. </template> 
  25. <script> 
  26. import { useStore } from "vuex" 
  27. import { computed } from 'vue' 
  28. export default { 
  29.  setup(){ 
  30.   const store = useStore() 
  31.   return
  32.    state:store.state, 
  33.    getDouble:computed(()=>store.getters.doubleNum) 
  34.   } 
  35.  } 
  36. </script> 

四、mutations 和 actions 的用法

調(diào)用 mutations 內(nèi)的方法時(shí),使用 commit 調(diào)用。上述的使用場(chǎng)景2 就是 mutations 方法的調(diào)用。

而 actions 異步更新 state 中的數(shù)據(jù),還是需要經(jīng)過(guò) mutations 。

  1. <template> 
  2.  <div> 
  3.   {{state.num}} 
  4.   <button @click="asyncUpdateNum">更新num</button> 
  5.  </div> 
  6. </template> 
  7.  
  8. <script> 
  9. import { useStore } from "vuex" 
  10. export default { 
  11.  setup(){ 
  12.   const store = useStore() 
  13.   return
  14.    state:store.state, 
  15.    asyncUpdateNum(){ 
  16.    store.dispatch('updateNum',88) 
  17.    } 
  18.   } 
  19.  } 
  20. </script> 

組件內(nèi)可以通過(guò) this.$store 屬性訪問(wèn)store容器,使用 composition API 可以通過(guò) useStore代替。其他的用法基本相同。

 

責(zé)任編輯:姜華 來(lái)源: 今日頭條
相關(guān)推薦

2021-10-29 07:47:35

Vue 3teleport傳送門組件

2020-12-18 09:17:27

分析設(shè)計(jì)師UI

2024-09-06 07:59:45

vuexVue.js模式

2009-09-25 10:24:40

Androind入門教OPhone

2021-08-04 13:19:28

云原生存儲(chǔ)編排

2014-06-04 10:42:34

Swift蘋果iOS

2021-05-20 07:26:21

工具Vuex Vue.js

2014-02-21 15:15:49

4G

2021-10-19 10:52:06

Serverless阿里云

2024-11-20 08:09:19

RabbitMQ項(xiàng)目客戶端

2017-08-01 15:25:41

LinuxNginxHttps

2010-08-16 09:48:38

無(wú)線局域網(wǎng)

2022-01-13 08:13:14

Vue3 插件Vue應(yīng)用

2014-04-22 10:49:25

上海移動(dòng)4GF1大獎(jiǎng)賽

2014-02-10 16:27:09

百萬(wàn)級(jí)IOPSOceanStor 1

2017-07-04 15:32:28

周年慶極速狂奔小米手機(jī)

2017-03-16 14:01:00

2010-06-02 08:53:51

.NET 4并行編程

2010-08-23 13:11:51

無(wú)線路由器配置

2014-11-16 21:59:54

NETGEAR無(wú)線路由
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)