淺談Vuex的狀態(tài)管理(全家桶)

Vuex 是一個專為 Vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理模式。它采用集中式存儲管理應(yīng)用的所有組件的狀態(tài),并以相應(yīng)的規(guī)則保證狀態(tài)以一種可預(yù)測的方式發(fā)生變化。Vuex 也集成到 Vue 的官方調(diào)試工具 devtools extension,提供了諸如零配置的 time-travel 調(diào)試、狀態(tài)快照導(dǎo)入導(dǎo)出等高級調(diào)試功能。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:主機(jī)域名、網(wǎng)站空間、營銷軟件、網(wǎng)站建設(shè)、歷城網(wǎng)站維護(hù)、網(wǎng)站推廣。

以上是vuex的官方文檔對vuex的介紹,官方文檔對vuex的用法進(jìn)行了詳細(xì)的說明。這里就不再細(xì)講vuex的各個用法,寫這篇博客的目的只是幫助部分同學(xué)更快地理解并上手vuex。

1. 安裝

$ npm install vuex --save

2. 在main.js 主入口js里面引用store.js

import Vue from 'vue'
import App from './App'
import router from './router' 
import store from './vuex/store'  //引用store.js
Vue.config.productionTip = false //阻止在啟動時生成生產(chǎn)提示 

//vue實例
new Vue({
 el: '#app',
 router,
 store,              //把store掛在到vue的實例下面
 template: '<App/>',
 components: { App }
})

3. 在store.js里引用Vuex

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex) //注冊Vuex

// 定義常量  如果訪問他的話,就叫訪問狀態(tài)對象
const state = {
  count: 1
}

// mutations用來改變store狀態(tài), 如果訪問他的話,就叫訪問觸發(fā)狀態(tài)
const mutations = {
  //這里面的方法是用 this.$store.commit('jia') 來觸發(fā)
  jia(state){
    state.count ++
  },
  jian(state){
    state.count --
  },
}
//暴露到外面,讓其他地方的引用
export default new Vuex.Store({
  state,
  mutations
})

4. 在vue組件中使用

使用$store.commit('jia')區(qū)觸發(fā)mutations下面的加減方法

<template>
 <div class="hello">
   <h2>Hello Vuex</h2>
   <h6>{{$store.state.count}}</h6>
   <p>
    <button @click="$store.commit('jia')">+</button>
    <button @click="$store.commit('jian')">-</button>
   </p>
 </div>
</template>

<!-- 加上scoped是css只在這個組件里面生效,為了不影響全局樣式 -->
<style scoped>
  h6{
   font-size: 20px;
   color: red;
  }
</style>

5. 查看演示

淺談Vuex的狀態(tài)管理(全家桶)

6. state訪問狀態(tài)對象

使用computed計算

<template>
 <div class="hello">
   <h2>Hello Vuex</h2>
   <h6>{{count}}</h6>
   <p>
    <button @click="$store.commit('jia')">+</button>
    <button @click="$store.commit('jian')">-</button>
   </p>
 </div>
</template>

<script>
import {mapState} from 'vuex'
export default{
  name:'hello', //寫上name的作用是,如果你頁面報錯了,他會提示你是那個頁面報的錯,很實用
  // 方法一
  // computed: {
  //  count(){
  //   return this.$store.state.count + 6
  //  }
  // }
  
  // 方法二 需要引入外部 mapState
  computed:mapState({
   count:state => state.count + 10
  })
 
  // ECMA5用法
  // computed:mapState({
  //  count:function(state){
  //   return state.count
  //  }
  // })
  
  //方法三
  // computed: mapState([
  //  'count'
  // ])
 }
</script>

7. mutations觸發(fā)狀態(tài) (同步狀態(tài))

<template>
 <div class="hello">
   <h2>Hello Vuex</h2>
   <h6>{{count}}</h6>
   <p>
    <button @click="jia">+</button>
    <button @click="jian">-</button>
   </p>
 </div>
</template>
<script>
import {mapState,mapMutations} from 'vuex'
 export default{
  name:'hello', //寫上name的作用是,如果你頁面報錯了,他會提示你是那個頁面報的錯,很實用
  //方法三
  computed: mapState([
   'count'
  ]),
  methods:{
   ...mapMutations([
     'jia',
     'jian'
   ])
  }
 }
</script>

8. getters計算屬性

getter不能使用箭頭函數(shù),會改變this的指向

在store.js添加getters

// 計算
const getters = {
  count(state){
    return state.count + 66
  }
}

export default new Vuex.Store({
  state,
  mutations,
  getters
})
//count的參數(shù)就是上面定義的state對象
//getters中定義的方法名稱和組件中使用的時候一定是一致的,定義的是count方法,使用的時候也用count,保持一致。
組件中使用

<script>
 import {mapState,mapMutations,mapGetters} from 'vuex'
 export default{
  name:'hello',
  computed: {
   ...mapState([
    'count'
   ]),
   ...mapGetters([
    'count'
   ])
  },
  methods:{
   ...mapMutations([
     'jia',
     'jian'
   ])
  }
 }
</script>

9. actions (異步狀態(tài))

在store.js添加actions

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

// 定義常量
const state = {
  count: 1
}

// mutations用來改變store狀態(tài) 同步狀態(tài)
const mutations = {
  jia(state){
    state.count ++
  },
  jian(state){
    state.count --
  },
}
// 計算屬性
const getters = {
  count(state){
    return state.count + 66
  }
}
// 異步狀態(tài)
const actions = {
  jiaplus(context){
    context.commit('jia') //調(diào)用mutations下面的方法
    setTimeout(()=>{
      context.commit('jian')
    },2000)
    alert('我先被執(zhí)行了,然后兩秒后調(diào)用jian的方法')
  },
  jianplus(context){
    context.commit('jian')
  }
}

export default new Vuex.Store({
  state,
  mutations,
  getters,
  actions
})

在組件中使用

<template>
 <div class="hello">
   <h2>Hello Vuex</h2>
   <h6>{{count}}</h6>
   <p>
    <button @click="jia">+</button>
    <button @click="jian">-</button>
   </p>
   <p>
    <button @click="jiaplus">+plus</button>
    <button @click="jianplus">-plus</button>
   </p>
 </div>
</template>
<script>
 import {mapState,mapMutations,mapGetters,mapActions} from 'vuex'
 export default{
  name:'hello',
  computed: {
   ...mapState([
    'count'
   ]),
   ...mapGetters([
    'count'
   ])
  },
  methods:{
   // 這里是數(shù)組的方式觸發(fā)方法
   ...mapMutations([
     'jia',
     'jian'
   ]),
   // 換一中方式觸發(fā)方法 用對象的方式
   ...mapActions({
    jiaplus: 'jiaplus',
    jianplus: 'jianplus'
   })
  }
 }
</script>

<style scoped>
  h6{
   font-size: 20px;
   color: red;
  }
</style>

10. modules 模塊

適用于非常大的項目,且狀態(tài)很多的情況下使用,便于管理

修改store.js

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const state = {
  count: 1
}
const mutations = {
  jia(state){
    state.count ++
  },
  jian(state){
    state.count --
  },
}
const getters = {
  count(state){
    return state.count + 66
  }
}
const actions = {
  jiaplus(context){
    context.commit('jia') //調(diào)用mutations下面的方法
    setTimeout(()=>{
      context.commit('jian')
    },2000)
    alert('我先被執(zhí)行了,然后兩秒后調(diào)用jian的方法')
  },
  jianplus(context){
    context.commit('jian')
  }
}

//module使用模塊組的方式 moduleA
const moduleA = {
  state,
  mutations,
  getters,
  actions
}

// 模塊B moduleB
const moduleB = {
  state: {
    count:108
  }
}

export default new Vuex.Store({
  modules: {
    a: moduleA,
    b: moduleB,
  }
})

淺談Vuex的狀態(tài)管理(全家桶)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)頁名稱:淺談Vuex的狀態(tài)管理(全家桶)
文章起源:http://www.muchs.cn/article18/gcepgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、虛擬主機(jī)、網(wǎng)站營銷、定制網(wǎng)站用戶體驗、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)