|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Vuex 的用法详解
8 I; `# q1 A% _* U f+ a
) S. T, _! C- b; M6 m7 ^' O本案例测试下Vuex的使用,创建一个新的web项目# r9 u, v/ R% i: u
Vue init webpack web
* y/ W/ H2 \. W4 v% F4 ?3 A安装vuex
0 [3 F; r" M$ g8 t$ `1 g$ F- Mnpm install vuex --save
3 J8 m. E* H4 n8 d$ [$ }- }$ B启动, npm run dev,打开localhost:8080 即可
; Y# g: e" K1 l* G! r9 s Z5 M* w' p7 z7 f, `. H
- ^; p1 |! _" z! Z. c$ G$ N(1)引入vuex
2 \* q. s7 Q$ G" y) w创建一个src/store文件夹,创建index.js , 引入并导出store
: d T( \' |! D. q5 l# j' z8 Oimport Vue from 'vue': j6 k" z; h) E# S k
import Vuex from 'vuex'
( Z! L* l/ @, _0 N F, i+ WVue.use(Vuex);) J& o+ `8 v& l/ e) p% E2 X* ?9 ]
const store = new Vuex.Store({6 R. ]0 | p7 G0 E7 ^
}); {8 y! ?9 C: c% m6 {2 u% S
export default store;
6 N( L4 D( ]& o( [& A0 a
& O9 L9 \! L9 L+ k) d0 p" s(2)引入store到main.js
4 v; S X+ R/ ximport Vue from 'vue'4 Q) r5 ~- U9 g' N% v5 \
import App from './App'% `& d H% ]- f! e: V; ~0 q" h* u
import store from './store'% S% D* ^$ S) S
3 z6 R5 ~( Z* {0 U
Vue.config.productionTip = false
n" N4 V4 Q5 w$ H; `, ^! @0 V
$ W8 {5 [5 y. x+ t, H5 Z% N/* eslint-disable no-new */3 U: v, G; G$ _& W
new Vue({
0 O# s7 b; W! z: |. Z; d: C el: '#app',
; r, r! D' ~4 }: t/ R5 u store,
9 M8 z" a, p1 v0 ^( S components: { App },
/ T. M) m( [7 m+ ?) g" v4 n template: '<App/>'9 i K: O: F% x% i z# w" b
})& o3 w5 A( B; U) N! f+ q1 Y" G
6 ?( y4 m( D* I+ e) B5 l3 l2 h* F
2 C: p. n% p) U/ w6 m
(3) 修改 helloword.vue ,先使用state获取值进行测试2 b0 u' V2 H$ f/ P: a2 d5 E6 {
State: vuex中的数据源,我们需要保存的数据就保存在这里,可以在页面通过 this.$store.state来获取我们定义的数据;
; O6 i/ Z6 \# ^" Oconst store = new Vuex.Store({: j4 z' _ f* H, w
state: {
7 Q, A) d' m1 u% | count:1
, Q2 L, h+ }( [ }6 f7 w( |- A$ T) |1 e5 @& U
})( s" Y3 z+ [1 c/ [
|2 z" Q7 e0 P/ ]1 n8 Z& x
helloword.vue----0 [3 D4 w, J$ n) \# L8 R. y
<template>( l% y( R' L, e9 U
<div class="hello">
9 l- ^ s# T! z; W
# g/ V2 b* h/ _% } <h2>{{this.$store.state.count}}</h2>
' D- i3 N" k% {2 s% E0 [2 u9 L% m3 _5 _$ g2 d
</div>
# |. k% o7 Q, Q B1 t7 w. q1 ^</template>
: m# C. {8 ] D9 X( B6 [5 x- ]' N, i0 l# C9 L
% z1 A4 m; n9 s$ O% P" f. X9 r: f显示结果为: 1
$ h ~4 ~" ?0 d2 g- F4 j5 m' ]0 M$ a9 A) N3 K9 N# Z
: [3 Z6 k" V R' [/ R
(4)Getter相当于vue中的computed计算属性,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算,这里我们可以通过定义vuex的Getter来获取,Getters 可以用于监听、state中的值的变化,返回计算后的结果,这里我们修改Hello World.vue文件如下:2 c+ s' T# ]* x* B: _0 F
" O+ L# |1 i8 N3 ` u7 h- o Hconst store = new Vuex.Store({
+ Z0 L; Y( A- j/ t5 H+ v, q state: {% d4 l, H; c9 P7 U6 E# _
count:1" t$ K( ]5 J4 h- ?; V7 k! n( {) V
},
9 O: y2 |! D& C+ U& u+ N getters:{
" h! o- [- V" x4 n; [* y: T1 U getStateCount:state=>{
$ f2 R6 b. u. n- I9 H# C return state.count + 1;$ b* N( m. v7 K' a8 m- W+ Q
}
1 n, c! W$ d+ ?; G/ W( e: P& t. Z }7 S; U3 _% k3 g" T
})
3 C8 e$ B. g3 U% X4 A* e# A4 T, t8 c( u' N& ?
<template>. x5 I" J. ]6 u$ d
<div class="hello">6 r; H4 p% S) X4 s! q+ G: S% r' _
6 w$ K5 a* E+ v# ?0 K+ d7 z <h2>StateValue: {{this.$store.state.count}}</h2>
# J" G$ Q4 n8 z- G <h2>GettesVaule: {{this.$store.getters.getStateCount}}</h2>$ F# Z( A! n; a) J4 ]2 l# b
</div>6 Q) X' O+ z/ v& c0 Z) Z
</template>. d8 t) h9 `! h# \; P; x/ Q
Y* {1 Z' E- e; J. n7 m. f
3 X* F7 d8 A8 o* c# K显示结果:5 i, E- u/ i1 T' W# N9 h% n
StateValue: 1GettesVaule: 26 w7 S) u, a( t, `3 @3 w
$ }+ z p4 ]% K0 i6 i(5)+ Q6 K, o3 `4 f8 U
Mutations: 数据我们在页面是获取到了,但是如果我们需要修改count值怎么办?如果需要修改store中的值唯一的方法就是提交mutation来修改,我们现在Hello World.vue文件中添加两个按钮,一个加1,一个减1;这里我们点击按钮调用addFun(执行加的方法)和reductionFun(执行减法的方法),然后在里面直接提交mutations中的方法修改值:
. S: X+ _, g1 \$ d7 p' v6 j修改index.js文件,添加mutations,在mutations中定义两个函数,用来对count加1和减1,这里定义的两个方法就是上面commit提交的两个方法如下:
1 e: @5 F$ Q$ a amutations:{
* N9 s2 c$ F p add(state){! o) p& N' [3 q2 `5 b
state.count = state.count + 1;5 i0 U* _1 J3 e) R6 ^
},& P) G* @3 U, j- d M' C
reduce(state){/ S# i* Q) d' t9 l
state.count = state.count - 1;
* h, W9 F, o, p7 A2 d- F+ p }4 c) J8 v3 h/ R
}
5 `; h+ Q4 l" ^/ g, k3 j<template>/ ~8 g6 j# n2 y1 A2 R
<div class="hello">
% P3 K( g: ?/ C4 R; z <h1>Actions:</h1>
" ^1 L1 T1 j- n& r <el-button type="primary" @click="addFunc()">Add</el-button>% r6 A1 h9 [9 k9 }* d) P$ \
<el-button type="Secondary" @click="reduceFunc()">Reduce</el-button>
/ Q4 g6 p7 ]3 H' u* F) O4 X <h2>StateValue: {{this.$store.state.count}}</h2>0 [) h6 d* R4 z4 ?0 h: _
<h2>GettesVaule: {{this.$store.getters.getStateCount}}</h2>( w# P2 k" `6 P6 q3 z
</div>
- `- g( V1 m. ~3 L7 m/ J</template>3 z: S9 j0 w# t9 F1 Q0 J- \
. v! r/ M% i' H0 k8 S6 s, V
$ W% S7 q! e& x6 b! |结果:9 a0 T$ g6 \) X
& ] Q+ B- d- D' b E- J
- j1 z1 M. a; C0 [* @8 W6 V1 h* NActions:Add ReduceStateValue: 5GettesVaule: 68 a# Y) C2 y) T' p# i
/ P1 F2 y4 N& G% h" S; X; S
4 Y' d' S9 A1 l. r& r0 x! ]
' Z: ^. J6 V2 n- l" ~
9 S+ c5 M& f6 g5 _, U; R8 H+ P0 c+ W7 R
( m+ L& Y0 x% Z; D, a" x6 W* H
8 }3 K# k7 b1 \) Y8 e* t" t$ k q
1 T3 r2 K. F. B, J: |0 J# v(6)Actions:. y$ t# ]# V, x
我们看到,当点击三次后值从2变成了-1;页面上的值是改变了;我们达到了修改store中状态值的目的,但是,官方并不介意我们这样直接去修改store里面的值,而是让我们去提交一个actions,在actions中提交mutation再去修改状态值,接下来我们修改index.js文件,先定义actions提交mutation的函数:
6 i* _8 [, L2 w; z0 m 9 e, c$ I' ` f
mutations:{
, P8 e2 c2 [- A+ c add(state){
9 B* E; K7 J5 O ` state.count = state.count + 1;
: i z4 _/ Q0 g8 w },
T Z8 \( U9 L/ y1 ?& p reduce(state){
6 n$ N' u8 G5 t8 s state.count = state.count - 1;
" Q2 ] Z$ f0 Y5 U& q& i }
, m3 L6 N9 h- Y6 W% | },
( k( h/ N; n" Y2 V. h actions:{, c- ^9 x3 f2 ?% u E" @# k; f$ t3 u( z
addFunc(context){. N; H: s# b0 n Z9 D
context.commit("add");+ ?4 P9 r1 ~. L: f
},+ Q* P' N* E W4 m
reduceFunc(context){3 p, I4 l, N+ h8 M8 O* E
context.commit("reduce");. i/ ?* g% p( F3 L* s
}' i( o) B+ s# I2 v1 a; z7 S4 i
}- @. X6 \+ \# M' L7 u
. `) o6 D7 C$ t" `/ f- @5 u% x
$ h$ S6 g% ]! }2 |( V! P
7 J }) a3 B. b" U- A% p methods:{
& s3 x. y% v% C addFunc(){' w# D* H+ z+ ]6 w5 R) b3 H+ c/ z
//this.$store.commit("add");
: t Y5 I, T9 Z# Z( D4 \' {7 t1 N this.$store.dispaTCh("addFunc");! k9 @1 U/ |5 f2 Y; A9 t
},
3 ~$ ` C# f9 ]' [* E reduceFunc(){
. x; R' Y# l7 Q* J //this.$store.commit("reduce");' \ x( j* n+ f5 {) N
this.$store.dispatch("reduceFunc");$ p& D! i2 w2 C
}6 t; y9 X8 o' m7 w
}" s" t. }: {0 B3 z0 E6 O0 t
$ V7 T, o, |3 D- J
这里我们把commit提交mutations修改为使用dispatch来提交actions;我们点击页面,效果是一样的
, V; S# H$ y- C, `: [2 r1 I- v实现效果一样!!!!
/ ?$ b6 x- t/ z! k1 I( \
$ w6 @5 ]( T' A8 M- Q. Z" R4 |0 o' u) D2 P1 R
+ D8 d9 k% o. {' v6 o. t6 a
' u7 n. m! l- \% Y6 z, K. U# s; ~: X
& C2 D/ H: n! Z( f
5 {9 j. u& H N3 [# _0 A
5 _* u* \5 i7 k0 g# \/ o9 g# @9 b
4 S! h$ a% W C
5 Z% Z' S0 w$ M$ R6 G" d4 o" W* h6 @% J2 V6 M
6 a$ R g: H8 e
: Y( k' S6 ~9 ^, J# o% ~' G
; R0 K/ x4 I5 ?2 n8 R9 q% n% l. U6 R
|
|