|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Vuex 的用法详解. C: O& C; H+ C
5 l' o& |4 p( ?9 R1 L本案例测试下Vuex的使用,创建一个新的web项目
$ a& p5 K J# O+ h$ XVue init webpack web
! y' ~8 L# i4 k D( N$ n安装vuex
- A$ X; m7 O4 nnpm install vuex --save
+ [# a1 J. H( G" z启动, npm run dev,打开localhost:8080 即可
, }7 s0 K* Y8 A' v& P9 T' `
! G1 L( F; o {7 k
2 [8 G3 u- h) h( Q( t: m9 O(1)引入vuex' w+ l8 f7 e* s$ T* B) ?1 w9 {5 s
创建一个src/store文件夹,创建index.js , 引入并导出store
3 ?; C: Y0 @7 f }% b5 Cimport Vue from 'vue'
9 t/ k- D0 F2 r: D5 V1 Oimport Vuex from 'vuex': j% \+ Z( H/ N1 |1 w. b1 `
Vue.use(Vuex);
! v2 _# v& _4 t3 x% {const store = new Vuex.Store({
: s' \0 |7 g! B})" @; h; M% v6 O- E4 V' c
export default store;
2 o' J* |! U" s+ |& ?6 o, y; s: k* f8 x
(2)引入store到main.js
0 `( O: c6 O& Z1 Y# h% \import Vue from 'vue'( [; j; J3 P) A& A8 n- T1 y
import App from './App'1 h" h6 r# v1 A& j
import store from './store'1 i. _" x: w; x+ w, c; `( o
. f" C( u0 p- j- _Vue.config.productionTip = false
; v* z0 N# L4 v
+ }, @5 }; X! x/* eslint-disable no-new */8 m4 R5 N1 j) S# ~
new Vue({
/ s3 e) V9 K4 e7 ~- a# \ el: '#app',
8 c* ^$ Q2 _% @% c8 [7 `6 d store,, M1 O0 v& o& A; @& I& x5 _- c
components: { App },
( K" @% U0 a9 m9 k template: '<App/>'
5 ]; L$ n8 E" j* e, D7 C})6 b1 r1 _$ p! F/ q/ d# h
, x1 Z* Z# v- e0 \; j: {; p
) G* T9 Z' e& ]/ x z& H( t; D3 |" Q
(3) 修改 helloword.vue ,先使用state获取值进行测试) L7 C# K% {- E9 _& ?; T, T
State: vuex中的数据源,我们需要保存的数据就保存在这里,可以在页面通过 this.$store.state来获取我们定义的数据;
. ~; T; Z n' Q6 A% ]/ gconst store = new Vuex.Store({- h r6 o$ V7 n. F& _
state: {
/ u9 P3 Y: R6 u U4 q- h! L9 L count:1
' ^/ B5 S+ \0 c0 O }2 n) I- g% _ A" g2 P
})
* K5 Q' }1 O. q3 }% r* T( o9 g
8 t$ B; P2 T5 E. ~- H" g+ Khelloword.vue----
?; ]5 c) o# s; s3 F& K<template>8 A9 h3 s# \; ^( u
<div class="hello">( Y( `/ c! I q) f+ D8 S6 \
2 R( S# q! V4 P/ S i7 ~ <h2>{{this.$store.state.count}}</h2>
6 M0 S" i: c) J6 |& t9 E( g+ A, s9 F3 H f3 F) M3 f0 A9 _
</div>6 e1 S( y/ }' u' I: }# f
</template>2 `: k7 ~; @- I& w
$ J% m; p- l% E- J: \& c# ] @# }& ?5 u) P
显示结果为: 1 2 }- ]3 C4 e6 x0 f
) v6 C2 D- B' O7 e+ j" ]
2 {+ J) h' l/ X+ W! G0 l(4)Getter相当于vue中的computed计算属性,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算,这里我们可以通过定义vuex的Getter来获取,Getters 可以用于监听、state中的值的变化,返回计算后的结果,这里我们修改Hello World.vue文件如下:
" J! Y* V2 |; L
1 d0 i5 T1 V- y% Cconst store = new Vuex.Store({. Q3 W m' k m( g' O# l
state: {
4 u5 m* x5 \$ Q0 O) ]) y. ` count:1
3 V" I( q# m2 t8 R8 I7 i9 Q },
- x4 P5 c0 q; R* i# S getters:{5 ^: F% I) w1 L7 E% b5 y
getStateCount:state=>{+ f$ l4 a+ b: C/ ]( T6 i5 a) u
return state.count + 1;
+ A, t/ e. n0 g7 T" U) k6 f4 C }
6 h/ R' B; O8 {0 H# Q- \' ` }. n* [( h1 H8 z& s) _
})
/ b: q3 E0 z! Z. }* x& D! }9 d' ?5 K5 l% m
<template>
0 t* a6 i5 q9 J, y <div class="hello">7 j/ _3 O, L- c; D8 O0 D6 `: }: v$ U$ n' e
# p1 L2 S4 ~4 @! |& ^4 A
<h2>StateValue: {{this.$store.state.count}}</h2>
3 o- r1 q. v% D% ^9 c- f <h2>GettesVaule: {{this.$store.getters.getStateCount}}</h2>
: Y$ m! y& E3 E" M </div>5 y% M, Q9 y. |8 I5 o- h* ?
</template>2 I% o% a3 c+ t# i1 b" y H
3 n2 x7 d) e. u% i
# u& R4 c* I& C: W0 Y显示结果:% R3 a4 V, q/ J- P: P* K2 Q
StateValue: 1GettesVaule: 2
; l- l1 V* q9 H/ o( K& m# ~/ W7 _$ T% I3 ~! c) R4 D8 G% f
(5)1 Z" J$ g; }+ X# w3 q6 m1 |2 ]
Mutations: 数据我们在页面是获取到了,但是如果我们需要修改count值怎么办?如果需要修改store中的值唯一的方法就是提交mutation来修改,我们现在Hello World.vue文件中添加两个按钮,一个加1,一个减1;这里我们点击按钮调用addFun(执行加的方法)和reductionFun(执行减法的方法),然后在里面直接提交mutations中的方法修改值: . _3 K2 G; R k" K$ k c
修改index.js文件,添加mutations,在mutations中定义两个函数,用来对count加1和减1,这里定义的两个方法就是上面commit提交的两个方法如下:9 K) W* c4 g* {6 o4 C& o0 B
mutations:{
& p; N8 O r9 R1 c# Z add(state){7 x+ A9 W3 u8 _& r! z D5 e: i, n5 Z
state.count = state.count + 1;. ]" ?/ d1 ]; h0 b7 E( I
},
. S9 P: Q/ O; J5 A, T6 p3 Z reduce(state){
( x+ W; ^6 m4 y1 J. N( ^2 D state.count = state.count - 1;
8 h0 a1 o" ^$ m3 G/ k" h }) S6 P1 G; J7 V) F; H) W) {
}8 \6 ?% y; {3 ?5 k: G
<template># B+ u5 w# y; e# ~' g1 E$ c6 V! ]: m
<div class="hello">6 D/ A" U/ ^# l( Y) q7 ~* p( M* S
<h1>Actions:</h1>
4 d6 ^9 X2 K ~- u/ ?2 E <el-button type="primary" @click="addFunc()">Add</el-button>
8 X6 U5 b2 V$ t3 N8 Q <el-button type="Secondary" @click="reduceFunc()">Reduce</el-button>
" S# v# q* B# D2 P/ w% b' U' [$ R <h2>StateValue: {{this.$store.state.count}}</h2>
0 `9 \7 { \8 Y- Z- L; \ <h2>GettesVaule: {{this.$store.getters.getStateCount}}</h2>: f P. y3 o; k$ W
</div>% r' F5 t9 `1 s
</template>
3 U. m4 J2 c' ~& I8 M8 t3 W4 j* c: o8 S, ^3 E
9 i5 P7 F3 g4 g
结果:2 }, g- J* v% R& x2 J s8 z7 k
3 m* m9 ]0 \* h" R$ ~+ o; X# U. U' w. \( B7 V2 U# _
Actions:Add ReduceStateValue: 5GettesVaule: 6
$ C& u: s. B) C( ]: T/ I; a5 v' s# V) r# ?; m" Z
5 T* d5 A; C7 Z1 L% v: b1 e+ V8 Y
9 O4 v/ z! j2 H j; P( j% z
: l. o% k! Y7 G: C1 U
% U& O, J) v" N1 E" i
, `$ A( X5 d# p7 K: O. V9 X B- q, F. w4 |4 c, M' J Q8 C
(6)Actions:$ _+ D2 k5 |7 z9 w6 M- M% x* ^+ X/ n
我们看到,当点击三次后值从2变成了-1;页面上的值是改变了;我们达到了修改store中状态值的目的,但是,官方并不介意我们这样直接去修改store里面的值,而是让我们去提交一个actions,在actions中提交mutation再去修改状态值,接下来我们修改index.js文件,先定义actions提交mutation的函数: ! K( c4 {2 ^: E* m) c1 W
1 C+ x0 n6 K# q1 F, Kmutations:{0 X2 o p" N- n
add(state){% d9 Y8 X. r+ u7 N
state.count = state.count + 1;0 k0 M% N: n T7 l$ u
},
* i2 l9 Q% u; d reduce(state){
( a4 C3 s) V2 B" U8 G8 y6 b- W- Q state.count = state.count - 1;
% j. k6 n1 i7 o/ a( Y }* H* y j9 }, w6 b
},. R1 y9 V+ c. }8 F3 j& u
actions:{- _# s0 u1 c' f2 w* [$ Y
addFunc(context){+ R2 C* K" n. l
context.commit("add");
1 @' l+ z% ~0 `+ @1 [- f6 X8 q8 | }, X! {% ]; \5 P% T) y* L" T, e
reduceFunc(context){8 @3 }0 m0 Y4 P$ {) y% h4 y9 l
context.commit("reduce");1 {+ n5 T' V# a1 h
}) L/ ]4 X9 D7 `* J g, r
}1 S9 g- M5 j/ t) _+ i' q
8 i# e& v- v2 w7 S+ ~) r7 s+ T% M. l# Y' l2 W: p
# o/ l' n: a* d: z+ x- g: G
methods:{
e5 J7 r. H' M, k; M& y addFunc(){( H8 f7 B8 h& ?
//this.$store.commit("add");3 K0 S, `6 k% S- D/ F7 N! k" X
this.$store.dispaTCh("addFunc");9 R9 s) l @" V: i4 J& n+ n
},; L6 a. n1 q$ j3 ]" \4 [. @) ?
reduceFunc(){, o% [$ g T. g
//this.$store.commit("reduce");
J7 O5 a. R; _' N' g/ |/ D this.$store.dispatch("reduceFunc");. C! }/ a3 ]3 i. E1 i
}
4 a2 P) |+ j* @1 b }
/ s% Q* T2 I/ U- b; S4 N- U0 ^/ [8 g. Q1 C. ]! w
这里我们把commit提交mutations修改为使用dispatch来提交actions;我们点击页面,效果是一样的
0 a$ M \: H0 M" ]! \% z实现效果一样!!!!8 Z7 ~& f# q6 i( F- L# v& w: E! b
& V7 J) G8 b A! O6 Q
* u) S4 j+ q6 X
( [! e, o; V6 O: o Q* P: h
1 z# k/ E! R6 X ~( u+ F7 o: L4 X0 I1 S- C
' S3 e9 m$ y. b* q, r4 t* [- S9 P' v6 P- E( c
. R0 `' R0 s' J& {0 P# b0 T" R( G
v9 ~9 s9 x( w( T7 @4 q2 C2 U( D4 F3 H$ {
8 o( ^( M! I; A1 n3 c6 `! j3 Y
$ B+ \ t* `- z' P* e' i' ~4 s
' W; Z0 Z$ I, V! r
9 O2 d( ~6 Y! n" c5 U2 ^, j* G* V
( q( I* s! T3 T6 N7 ?" o9 j
|
|