Vuex簡介

一、什么是vuex

vuex是適用于在Vue項(xiàng)目開發(fā)時(shí)使用的狀態(tài)管理工具。

二、vuex的作用

試想一下,如果在一個(gè)項(xiàng)目開發(fā)中頻繁的使用組件傳參的方式來同步data中的值,一旦項(xiàng)目變得很龐大,管理和維護(hù)這些值將是相當(dāng)棘手的工作。為此,Vue為這些被多個(gè)組件頻繁使用的值提供了一個(gè)統(tǒng)一管理的工具——VueX。在具有VueX的Vue項(xiàng)目中,我們只需要把這些值定義在VueX中,即可在整個(gè)Vue項(xiàng)目的組件中使用。

三、vuex的安裝

1、安裝指令:

npm install vuex 或者 cnpm install vuex

2、創(chuàng)建vuex目錄
在項(xiàng)目的src文件夾內(nèi)新建一個(gè)store文件夾,在該文件夾內(nèi)再創(chuàng)建index.js
此時(shí)你的項(xiàng)目的src文件夾應(yīng)當(dāng)是這樣的


image.png

3、開始使用vuex
設(shè)置store中的index.js

import Vue from 'vue'
import Vuex from 'vuex'
import createPersist from 'vuex-localstorage'
Vue.use(Vuex)
//創(chuàng)建vuex構(gòu)造器
export default new Vuex.Store({
    state:{
        
    },
    mutations:{

    },
    plugins:[//長久儲(chǔ)存
        createPersist(
            {namespace:'namespace-for-state'}
        )
    ],
    modules:{
    }
})

設(shè)置main.js

import store from './store/index'//引入vuex
new Vue({
  el: '#app',
  store:store,
  components: { App },
  template: '<App/>'
})

四、vuex的的使用

在組件中使用Vuex

<template>
    <div id='app'>
        name:
        <h1>{{ $store.state.name }}</h1>
    </div>
</template>

或者要在組件方法中使用

methods:{
    add(){
      console.log(this.$store.state.name)
    }
},

VueX中的核心內(nèi)容
在VueX對(duì)象中,其實(shí)不止有state,還有用來操作state中數(shù)據(jù)的方法集,以及當(dāng)我們需要對(duì)state中的數(shù)據(jù)需要加工的方法集等等成員。
state 是存放狀態(tài) 和 Vue 實(shí)例中的 data遵循相同的規(guī)則
使用如下:

import Vue from 'vue'  
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state:{ //聲明數(shù)據(jù)
        str:'',
        obj:{},
        arr:[],
        num:0,
        bool:true/false
    },
})

getters 是加工state成員給外界 ,可以認(rèn)為是 store 的計(jì)算屬性。getter 的返回值會(huì)根據(jù)它的依賴被緩存起來,且只有當(dāng)它的依賴值發(fā)生了改變才會(huì)被重新計(jì)算。
使用方法如下:

import Vue from 'vue'  
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state:{ //聲明數(shù)據(jù)
        shopCar:[]  
    },
getters:{ //可以認(rèn)為是 store 的計(jì)算屬性
        shopCarPrices(state){
            var sum=0
            for(var i=0;i<state.shopCar.length;i++){
                sum+=state.shopCar[i]
            }
            return sum
        }
    }
})

組件中調(diào)用

this.$store.getters.shopCarPrices

mutations是對(duì) state成員操作 , Vuex 的 store 中的狀態(tài)的唯一方法是提交 mutation。
使用方法如下:

import Vue from 'vue'  
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state:{ //聲明數(shù)據(jù)
       name:'helloVueX'   
   },
mutations:{
       amend(state){
state.name = '張三'
       }
   }
})

而在組件中,我們需要這樣去調(diào)用這個(gè)mutation——例如在App.vue的某個(gè)method中:

this.$store.commit('amend')

使用Mutation傳值

this.$store.commit('amend',15)//傳一個(gè)值時(shí)
this.$store.commit('amend',{age:15,sex:'男'})//傳多個(gè)值時(shí)

接收掛載的參數(shù):

  amend(state,val){
    state.name = '張三'
         console.log(val) // 15或{age:15,sex:'男'}
  }

actions 是用來專門進(jìn)行異步操作,最終提交mutation方法。
由于setTimeout是異步操作,所以需要使用actions

import Vue from 'vue'  
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state:{ 
       key:''
    },
mutations: {
    updateKey(state,val){
        state.key=val
        console.log(state.key);
    }
  },
actions:{
    updateKey(state,val){
        setTimeout(()=>{
            state.commit('updateKey',val)
        },10)
    }
  }
})

在組件中調(diào)用:

this.$store.dispatch('updateKey',10)

modules 模塊化狀態(tài)管理,每個(gè)模塊擁有自己的 state、mutation、action、getter、甚至是嵌套子模塊——從上至下進(jìn)行同樣方式的分割。當(dāng)項(xiàng)目龐大,狀態(tài)非常多時(shí),可以采用模塊化管理模式。
在創(chuàng)建的store文件夾中在創(chuàng)建一個(gè)user.js文件。
在store中的index.js文件中引入剛創(chuàng)建好的user.js文件

import user from './user'

在index文件中寫入

 modules:{
        user
    },

創(chuàng)建好的user.js文件中寫入模塊,可以寫如我們需要的核心對(duì)象,語法都是一樣的
比如:

export default {
    state:{
           username :""
    },
    mutations:{
           add(state,val){
           state.username = val
        },
    },
}

組件中傳值

 this.$store.commit('add',res) 
  this.$store.commit('方法名',值) 

組件中調(diào)用

this.$store.state.user.username 
user代表的是在這個(gè)user模塊里 
username 代表的是在user這個(gè)模塊的state中有一個(gè)叫username 的變量

plugins是在vue中使用一些插件 列如可以使用vuex-localstorage
vuex-localstorage 要先進(jìn)行下載 可以使用

npm install vuex-localstorage或cnpm install vuex-localstorage

index.js中引入 vuex-localstorage

import createPersist from 'vuex-localstorage'

使用方法如下:

plugins:[
        createPersist({namespace:"namespace-for-state"})
],
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Vuex 是一個(gè)專為 Vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理模式。它采用集中式存儲(chǔ)管理應(yīng)用的所有組件的狀態(tài),并以相應(yīng)...
    Master_Devil閱讀 201評(píng)論 0 0
  • vuex是什么? 是一個(gè)專為 Vue.js 應(yīng)用程序開發(fā)的 狀態(tài)管理器。 采用集中式存儲(chǔ)管理應(yīng)用的所有組件的狀態(tài),...
    Elvmx閱讀 1,771評(píng)論 4 16
  • 每一個(gè) Vuex 應(yīng)用的核心就是 store(倉庫)?!皊tore”基本上就是一個(gè)容器,它包含著你的應(yīng)用中大部分的...
    GFI_ZT閱讀 212評(píng)論 0 0
  • 文章目錄 一、Vuex概述[https://blog.csdn.net/u010358168/article/de...
    peterz博客閱讀 1,017評(píng)論 2 8
  • 一.VueX 1. 關(guān)于 VueX VueX是適用于在Vue項(xiàng)目開發(fā)時(shí)使用的狀態(tài)管理工具。試想一下,如果在一個(gè)項(xiàng)目...
    Kinderzhu閱讀 711評(píng)論 0 4

友情鏈接更多精彩內(nèi)容