欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年11月04日 10:21:35   作者:拯救宇宙是我的使命  
本篇文章主要介紹了淺談Vuex狀態(tài)管理(全家桶),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

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

以上是vuex的官方文檔對(duì)vuex的介紹,官方文檔對(duì)vuex的用法進(jìn)行了詳細(xì)的說(shuō)明。這里就不再細(xì)講vuex的各個(gè)用法,寫(xiě)這篇博客的目的只是幫助部分同學(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 //阻止在啟動(dòng)時(shí)生成生產(chǎn)提示 

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

3. 在store.js里引用Vuex

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

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

// mutations用來(lái)改變store狀態(tài), 如果訪問(wèn)他的話,就叫訪問(wèn)觸發(fā)狀態(tài)
const mutations = {
  //這里面的方法是用 this.$store.commit('jia') 來(lái)觸發(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">
   <h1>Hello Vuex</h1>
   <h5>{{$store.state.count}}</h5>
   <p>
    <button @click="$store.commit('jia')">+</button>
    <button @click="$store.commit('jian')">-</button>
   </p>
 </div>
</template>

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

5. 查看演示

6. state訪問(wèn)狀態(tài)對(duì)象

使用computed計(jì)算

<template>
 <div class="hello">
   <h1>Hello Vuex</h1>
   <h5>{{count}}</h5>
   <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', //寫(xiě)上name的作用是,如果你頁(yè)面報(bào)錯(cuò)了,他會(huì)提示你是那個(gè)頁(yè)面報(bào)的錯(cuò),很實(shí)用
  // 方法一
  // 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">
   <h1>Hello Vuex</h1>
   <h5>{{count}}</h5>
   <p>
    <button @click="jia">+</button>
    <button @click="jian">-</button>
   </p>
 </div>
</template>
<script>
import {mapState,mapMutations} from 'vuex'
 export default{
  name:'hello', //寫(xiě)上name的作用是,如果你頁(yè)面報(bào)錯(cuò)了,他會(huì)提示你是那個(gè)頁(yè)面報(bào)的錯(cuò),很實(shí)用
  //方法三
  computed: mapState([
   'count'
  ]),
  methods:{
   ...mapMutations([
     'jia',
     'jian'
   ])
  }
 }
</script>

8. getters計(jì)算屬性

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

在store.js添加getters

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

export default new Vuex.Store({
  state,
  mutations,
  getters
})
//count的參數(shù)就是上面定義的state對(duì)象
//getters中定義的方法名稱(chēng)和組件中使用的時(shí)候一定是一致的,定義的是count方法,使用的時(shí)候也用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用來(lái)改變store狀態(tài) 同步狀態(tài)
const mutations = {
  jia(state){
    state.count ++
  },
  jian(state){
    state.count --
  },
}
// 計(jì)算屬性
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">
   <h1>Hello Vuex</h1>
   <h5>{{count}}</h5>
   <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ā)方法 用對(duì)象的方式
   ...mapActions({
    jiaplus: 'jiaplus',
    jianplus: 'jianplus'
   })
  }
 }
</script>

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

10. modules 模塊

適用于非常大的項(xiàng)目,且狀態(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,
  }
})

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue結(jié)合AntV?G2的使用踩坑記錄

    vue結(jié)合AntV?G2的使用踩坑記錄

    這篇文章主要介紹了vue結(jié)合AntV?G2的使用踩坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue中data數(shù)據(jù)之間如何賦值問(wèn)題

    vue中data數(shù)據(jù)之間如何賦值問(wèn)題

    這篇文章主要介紹了vue中data數(shù)據(jù)之間如何賦值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。
    2022-09-09
  • Vue 滾動(dòng)行為的具體使用方法

    Vue 滾動(dòng)行為的具體使用方法

    本篇文章主要介紹了Vue 滾動(dòng)行為的具體使用方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • elementUI動(dòng)態(tài)嵌套el-form表單校驗(yàn)舉例詳解

    elementUI動(dòng)態(tài)嵌套el-form表單校驗(yàn)舉例詳解

    最近工作遇到個(gè)需求,表單可以進(jìn)行增加刪除操作,需要進(jìn)行表單校驗(yàn),這篇文章主要給大家介紹了關(guān)于elementUI動(dòng)態(tài)嵌套el-form表單校驗(yàn)的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-06-06
  • vue模態(tài)框?qū)崿F(xiàn)動(dòng)態(tài)錨點(diǎn)

    vue模態(tài)框?qū)崿F(xiàn)動(dòng)態(tài)錨點(diǎn)

    這篇文章主要為大家詳細(xì)介紹了vue模態(tài)框?qū)崿F(xiàn)動(dòng)態(tài)錨點(diǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • 基于VUE實(shí)現(xiàn)的九宮格抽獎(jiǎng)功能

    基于VUE實(shí)現(xiàn)的九宮格抽獎(jiǎng)功能

    這篇文章主要介紹了基于VUE實(shí)現(xiàn)的九宮格抽獎(jiǎng)功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-09-09
  • electron-vite工具打包后如何通過(guò)內(nèi)置配置文件動(dòng)態(tài)修改接口地址

    electron-vite工具打包后如何通過(guò)內(nèi)置配置文件動(dòng)態(tài)修改接口地址

    使用electron-vite?工具開(kāi)發(fā)項(xiàng)目打包完后每次要改接口地址都要重新打包,對(duì)于多環(huán)境切換或者頻繁變更接口地址就顯得麻煩,這篇文章主要介紹了electron-vite工具打包后通過(guò)內(nèi)置配置文件動(dòng)態(tài)修改接口地址實(shí)現(xiàn)方法,需要的朋友可以參考下
    2024-05-05
  • vue項(xiàng)目之頁(yè)面class不生效問(wèn)題及解決

    vue項(xiàng)目之頁(yè)面class不生效問(wèn)題及解決

    這篇文章主要介紹了vue項(xiàng)目之頁(yè)面class不生效問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • VUE實(shí)現(xiàn)吸底按鈕

    VUE實(shí)現(xiàn)吸底按鈕

    這篇文章主要為大家詳細(xì)介紹了VUE實(shí)現(xiàn)吸底按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • Vue與Node.js通過(guò)socket.io通信的示例代碼

    Vue與Node.js通過(guò)socket.io通信的示例代碼

    這篇文章主要介紹了Vue與Node.js通過(guò)socket.io通信的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07

最新評(píng)論