Vuex的基本方法

2021-08-29 02:59:01 字數 2056 閱讀 9124

1、vuex是中心狀態管理方案,vuex裡面的store相當於乙個倉庫,用來儲存資料的。

2、const store = new vuex.store(, //呼叫 this.sto

re.s

tate

.屬性名

muta

tion

s:,/

/thi

s.

store.state.屬性名 mutations:{}, //this.

store.

stat

e.屬性

名mut

atio

ns:,

//th

is.store.commit(「方法名」,引數) 同步呼叫

getters:{}, to

re.g

ette

rs("

方法名"

,引數)

acti

on:/

/thi

s.

store.getters("方法名",引數) action:{} //this.

store.

gett

ers(

"方法名

",引數

)act

ion:

//th

is.store.dispatch(「方法名」,引數) 非同步呼叫

})3、給物件新增新屬性

vue.set(obj,『newprop』,123)

state.obj =

4、只要store.state裡面的值發生改變,就會立即觸發更新相關聯的dom,也會重新計算store.getters

5、使用mapstate

import from 『vuex』

export default)

}呼叫方法:this.count()相當於this.sto

re.g

ette

rs()

6、使用

常量代替

muta

tion

事件型別

impo

rtma

pmut

atio

nfro

m′vu

ex′e

xpor

tdef

ault

meth

ods:

mapm

utat

ion(

[′in

crea

ment

′,′i

ncre

amen

tly′

])呼叫

方法:t

his.

incr

eame

nt()

相當於t

his.

store.getters() 6、使用常量代替mutation事件型別 import from 'vuex' export default } 呼叫方法:this.increament()相當於this.

store.

gett

ers(

)6、使

用常量代

替mut

atio

n事件類

型imp

ortm

apmu

tati

onfr

om′v

uex′

expo

rtde

faul

tmet

hods

:map

muta

tion

([′i

ncre

amen

t′,′

incr

eame

ntly

′])調

用方法:

this

.inc

ream

ent(

)相當於

this

.store.commit()

7、分發action 使用store.dispatch

action提交給mutation,mutation更改state值

vuex的基本用法

1 this.sto re 我 們可以通 過thi s.store 我們可以通過 this.store 我們可以 通過th is.store 在vue的元件中獲取 vuex的例項。2 state vuex中的資料來源,我們可以通過this.store.state 獲取我們在vuex中宣告的全域性變數...

vuex的基本使用

vuex的特點 1.多元件共享狀態 多個元件使用同乙個資料 2.任何乙個元件發生改變,其他元件也要跟著發生相應的變化 基本使用 安裝vuex npm install vuex 建立例項 import vuex from vuex import vue from vue vue.use vuex co...

vuex的基本使用

首先先建立乙個store.js 安裝vue,vuex什麼的就不說了 這是乙個倉庫 import vue from vue import vuex from vuex vue.use vuex 裝載資料的乙個倉庫 const state 修改倉庫的方法 const mutations reduce s...