|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Vuex 的用法详解
& I0 ^6 s9 x# A! ~+ m, j. A, c" t: }! h- g2 T, C, r
本案例测试下Vuex的使用,创建一个新的web项目
: a, E9 l3 x( ~$ n- }& ~Vue init webpack web
9 V _( s6 ?4 I' y安装vuex0 T0 o X. F" S S/ P
npm install vuex --save/ D6 {( E. O3 b. e, O
启动, npm run dev,打开localhost:8080 即可
6 L D, a1 M0 r/ s I P8 D( T, e8 ^/ _
2 y Y8 s, r! S2 Z' `# s
(1)引入vuex
1 U+ j& E" w) X6 g( T创建一个src/store文件夹,创建index.js , 引入并导出store. \; x9 b. l6 I- t5 n B8 O
import Vue from 'vue'
! s1 N+ _7 W, A. o. W' _8 w. Bimport Vuex from 'vuex'
: |7 c5 F% w" ^# Q% {3 P0 y7 `Vue.use(Vuex);( F) ^9 x; e2 P4 E; @" g; R
const store = new Vuex.Store({
3 ]7 [: c0 @" _8 D* T: |' b})
- H% I9 T, [2 b' R3 |1 C: `export default store;
W* ^* q" N( \( M P; p3 |
% o. w6 ]5 e' d6 r(2)引入store到main.js8 n+ o3 p0 [ \7 h3 v5 X2 i8 Y
import Vue from 'vue'
; ^! Y* D+ Y, Z) [% w0 f' Eimport App from './App'
; v n/ H& ~: ]- ?7 E6 r1 ~, Gimport store from './store'
, N% G" ?7 ^1 l& y+ `3 V, n* s
: w2 R% V5 n' H# l' uVue.config.productionTip = false
, N- u) J/ [$ R/ @8 K% F# p0 p
/* eslint-disable no-new */: Q( Q% d: o" R# n9 `: B9 A+ \
new Vue({! b- m7 c: j- k$ w: L# m
el: '#app',6 A0 p% y0 V( P5 _: z* E
store,
! c3 k* O* C2 N- G* p- C components: { App },) _4 r7 H, [; G1 d; ?/ [
template: '<App/>'9 I& ]" r8 a/ V8 Z! M
})
, m' H! h9 i4 j- d g/ ]
, K3 S! c% H0 V" {' E4 E5 A; ^- [! A# d8 l1 f, ?5 z" h/ @
& S2 X% g. S, m3 b5 A- O1 P+ C* q j8 B(3) 修改 helloword.vue ,先使用state获取值进行测试
5 }6 t& r5 c2 F6 N8 bState: vuex中的数据源,我们需要保存的数据就保存在这里,可以在页面通过 this.$store.state来获取我们定义的数据;
# d3 f- b& I$ Aconst store = new Vuex.Store({( d% |' n/ \9 }! v' c! [( U
state: {
( n2 G" s6 F, P2 p. X# U count:1
7 G2 L: ?: H# n3 w1 b }; x: Q- ]0 D7 d8 S
})" C. \2 e9 ?7 D' I
9 }) w) o. A0 w4 lhelloword.vue----, a" K: p/ X" A; ~( I5 r/ {
<template>
" j+ H- B) v* L0 k; Y% \ <div class="hello">
/ L; P2 R, i/ J% S4 ^: q8 w
$ f7 M% k8 W* x2 y; H <h2>{{this.$store.state.count}}</h2>
0 K% d8 Z" O! W+ b- M& S$ }: P3 b; J, z4 Z
</div>6 P N: v7 V( C
</template>, A( H( v6 z5 P! f; s% b
& V& M: h+ S- B7 g8 a( @3 n) d7 ~
: L8 c& Q& p3 R# J显示结果为: 1
( Q. p* o8 P; b8 v. |8 @0 D Z
5 M8 G3 l; u* J1 e7 c! E" d: b5 ~% ] E* p# Z0 f6 H; i5 W' u/ Y% c8 s
(4)Getter相当于vue中的computed计算属性,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算,这里我们可以通过定义vuex的Getter来获取,Getters 可以用于监听、state中的值的变化,返回计算后的结果,这里我们修改Hello World.vue文件如下:
2 c* x% z" A: C( e: p! X1 }
0 _4 ?% y8 b. E8 Iconst store = new Vuex.Store({2 [1 ^; O+ F& v/ q4 b
state: {7 Q j- D! C" m8 B- ?
count:1' @9 u- x. s% D- L
},& j F* b; @! B* g( A
getters:{
, }9 s! Y0 d' l' A" C getStateCount:state=>{
# M7 {. G# ^+ \- B2 V return state.count + 1;8 X2 w- z& [- O. M1 O' a \0 j; R; `) `8 o
}) F5 k+ b3 H$ f
}6 A" D6 a. ?! b! C+ x$ a5 `
})/ n, ] g6 ^, v( ~5 J) H& E1 o' ]) g
* r% Q! X* n- E
<template>
; B. Y, |+ y! ?8 Z6 z <div class="hello">( t; j& ?" W: i4 T7 z% p
" |7 v5 }/ s3 [" b7 p* w
<h2>StateValue: {{this.$store.state.count}}</h2>( H5 @! R6 o5 H% K' }( w3 M3 |
<h2>GettesVaule: {{this.$store.getters.getStateCount}}</h2>+ N1 B8 D0 ^) _7 S& q7 v& ^8 N
</div># h. H6 V4 u1 J* u
</template>0 R+ r/ e5 g( A/ w
" q5 g$ E# g( F+ v6 G
n9 `0 D1 }8 Q' M显示结果:! m: K% O3 j. e2 |. r
StateValue: 1GettesVaule: 2
% c. q& U$ r! j4 D& _) s
. R; ~: e0 r' |# c(5)" p9 n9 l0 h3 ~1 _7 q4 Z
Mutations: 数据我们在页面是获取到了,但是如果我们需要修改count值怎么办?如果需要修改store中的值唯一的方法就是提交mutation来修改,我们现在Hello World.vue文件中添加两个按钮,一个加1,一个减1;这里我们点击按钮调用addFun(执行加的方法)和reductionFun(执行减法的方法),然后在里面直接提交mutations中的方法修改值:
* e6 v& S1 B6 U修改index.js文件,添加mutations,在mutations中定义两个函数,用来对count加1和减1,这里定义的两个方法就是上面commit提交的两个方法如下:3 Y) k- z! H, U8 K5 e: o
mutations:{
) | D; y# J3 t$ F4 }* u add(state){
/ B0 z {) p4 X( ^! e& D state.count = state.count + 1;
, Q7 a5 T' ?- C/ s- T6 J },
7 \# g' Y/ X# N9 f- a( p reduce(state){
+ g8 a, ~' O' `( F3 c state.count = state.count - 1;1 k8 _ J. I: a F' F
}8 [* K6 o: n' H j
}; f( }3 j: _# f _
<template>4 r! x( X, O3 [0 k: D4 g
<div class="hello">
/ n" g5 I9 q% O' V; ? <h1>Actions:</h1>, s3 \* T% h6 r3 H. w% d- y
<el-button type="primary" @click="addFunc()">Add</el-button>5 K/ T2 ]; ?6 t3 k7 _* l( Y& d' M+ v2 l- _
<el-button type="Secondary" @click="reduceFunc()">Reduce</el-button>$ S* O$ |0 ]3 R4 T
<h2>StateValue: {{this.$store.state.count}}</h2>% {9 M" v7 J3 H# L$ s9 z
<h2>GettesVaule: {{this.$store.getters.getStateCount}}</h2>
# A$ Z8 M9 N3 Y </div>; H. u7 v6 s; D4 w
</template>; J0 d# f2 g R2 b3 r' l
5 X" ~8 b% j$ R* C t. s3 k% P
- P2 k/ ?+ k: `, i% L结果:: L: B j4 L1 Q+ @6 u
% m0 }3 m" v0 s- t( u
: W5 O" C. W2 T3 A! ]0 v5 ]( @Actions:Add ReduceStateValue: 5GettesVaule: 6( [4 M5 P' K6 l
% n7 Q0 w7 m) x9 P7 ~6 [1 |* g$ W! ^1 b+ V7 _
! l9 F" T/ @9 u: J! |$ w
8 S [ G* L) x& S& B- @
- r; a7 |' g! k, t& z
) F0 }6 t$ M% P2 e3 C
) o* ~+ [5 m( B7 |
% s/ i( k$ f* ](6)Actions:3 y) @% [+ s6 c7 B3 x& b
我们看到,当点击三次后值从2变成了-1;页面上的值是改变了;我们达到了修改store中状态值的目的,但是,官方并不介意我们这样直接去修改store里面的值,而是让我们去提交一个actions,在actions中提交mutation再去修改状态值,接下来我们修改index.js文件,先定义actions提交mutation的函数: 6 h+ L% N+ L7 p6 Q/ {) o5 x
( }$ [2 D6 Q( h; K* }( {
mutations:{
( E N* _5 G& Q; F5 R+ n9 E) x add(state){
9 F- M, ?) f$ p7 L7 I% E* E state.count = state.count + 1;$ D) v' M4 j8 e: Q( U, Y9 |; t
},, e2 ~& o; O& j
reduce(state){0 W" `7 ?' M: v1 _% k d
state.count = state.count - 1;
/ k* Q6 H+ i, I3 g$ r S( f' T) i }" h4 W+ K6 c" R- B3 Y
},. f; }/ }, {# L& D. `
actions:{
7 i, `" x6 Y6 I3 p7 V! k! `' j addFunc(context){
! x( j7 p5 T( j" e context.commit("add");
( j1 G( V6 V7 I9 k. s },8 ]9 M5 w6 Y- p% w
reduceFunc(context){* h/ @( f" _5 R, `7 z# U, k% Y3 C
context.commit("reduce");. `3 k+ S' n( t/ o: ~7 ]) }
}
* }! ?% |2 `6 ?" c" ^& Y }) a. q* |% u( P @, u2 \
8 R' L, ~( @* i9 x1 E* R$ |# B
, O$ Z+ x5 T7 ~3 c 9 T: ~( P0 w5 w* A+ O
methods:{
! w" R4 d ~% F ^' G0 G4 | addFunc(){
8 O9 ?: h! g0 o3 \5 Z0 N5 I M //this.$store.commit("add");$ j: n& g3 c7 \5 ]- w
this.$store.dispaTCh("addFunc");9 y+ k* p- [* r$ O6 y
},
/ S, z5 |+ C7 Y# Y) m: |4 e- U( W+ m reduceFunc(){- s7 ~6 K% `9 w" s3 M) `
//this.$store.commit("reduce");
1 J& E/ Q; m' L this.$store.dispatch("reduceFunc");* I1 k' y! C' I( x& z
}8 P$ ~# m9 a$ r9 U# r6 `6 W6 ]* V: f
}
: f" \& w3 Q1 C* y/ j; k+ i! Z4 p/ Q5 F) F2 g% {
这里我们把commit提交mutations修改为使用dispatch来提交actions;我们点击页面,效果是一样的
! _7 T" N* q5 p; i; m" m8 _4 r实现效果一样!!!!
! I$ G$ b) G8 ]# i5 a
5 C1 q4 o- b6 n$ B& v; \' E5 C# H. ~+ u, G
7 c: \1 ^) G; Z! q
' y9 _: l( u0 o# P( A/ _
D2 U! D \+ f. j F5 v% v
. v, f, @/ V6 C* E g) T, ?: k; q4 O
4 x( [) e y* l" T
5 C: f1 D2 @7 Q, j& B* a: b6 I/ P( j% M+ z6 s
' }; }2 E2 l# v( N( _* f5 F$ P$ s" l5 l& n C. O" [. Y8 l
* G4 ~) x5 \. y) ~
% I% a" ]- a; g( _! ?0 V8 G5 N' o( d1 X
" j3 N! R/ y" f* V& w) ?" O+ ~ |
|