|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
×
Vuex 的用法详解/ ~. l/ R4 M* A% A! H' a4 v) o
- @6 J/ y$ C7 h4 z
本案例测试下Vuex的使用,创建一个新的web项目
) G+ V; Z9 e& j8 h4 P" PVue init webpack web; k2 N7 I, \: F; D* l* w, d
安装vuex
% R, Q' H% ~) G! s9 w5 D2 }# knpm install vuex --save s, V3 k9 I3 h7 v
启动, npm run dev,打开localhost:8080 即可! L; H {& o0 v8 _
8 }4 a+ Z0 _* |6 D
9 j: T6 f( R' O) j: Y- W0 S(1)引入vuex
. U9 X2 \5 J) E' u创建一个src/store文件夹,创建index.js , 引入并导出store
1 f3 T- D3 t' u b' _: Nimport Vue from 'vue'# w0 u8 q6 ?" {, N9 {5 P% ?# W# |9 l( D
import Vuex from 'vuex'
, }% \: V# f. h' m2 ?Vue.use(Vuex);# j4 f% X3 J' b. z% D$ k
const store = new Vuex.Store({
3 f7 g& n+ a7 Y5 I3 h})
9 q: X7 t. W0 U G0 B& Z1 c7 uexport default store;
! ]: [! H9 @3 d: c' g8 k$ T; X1 S+ \. ?5 {+ R: w2 e% \
(2)引入store到main.js
% A* ]" c' ]1 }0 T; timport Vue from 'vue'
; }0 Q- e" w; q6 \* X$ Dimport App from './App'3 `* U6 W4 k3 S* c( a" V Z0 `, ]
import store from './store' ^! O6 U2 k# \7 n ]6 x [9 [2 ]( T( v
V7 I& l% g/ OVue.config.productionTip = false
1 c/ S" p" S+ b" i* P1 x" x7 E& A0 T
/* eslint-disable no-new */. [* p8 Z3 y) r' x
new Vue({/ ` S' T/ s3 S# E. H
el: '#app',
. [- P, c6 E/ V2 n' I0 ~! ?- J% @ store,3 S9 K( [, J$ x) X0 }2 ~
components: { App },
2 W: H% B% v! d3 U, s template: '<App/>'
1 O. X6 d, k7 m6 {4 G})* L/ ~$ t6 p& \
" x8 s" R, q ?; D; F* w2 H$ w/ ~0 b3 A0 q: A4 ^
|5 j' s1 C) B8 h
(3) 修改 helloword.vue ,先使用state获取值进行测试. \( o' p: Z F5 t, }
State: vuex中的数据源,我们需要保存的数据就保存在这里,可以在页面通过 this.$store.state来获取我们定义的数据;
; H4 ^( _$ z: ^+ O+ N9 M% Y" `- v" Bconst store = new Vuex.Store({ W9 |: H3 c! g, u2 I0 r- g
state: {
9 [! @5 M* k4 r0 B) ` count:1. d* O$ \4 F/ j, e6 ^7 R% c2 k/ n
}) o$ Z+ D6 M4 V( S& n4 P
})
; ^& I. J! n6 T. v: s. T9 r/ ^2 g' r+ ?( y" A% B% e1 W& r
helloword.vue----) g( }* G" w( w9 B+ U* D
<template>
4 o e- p3 O. O/ |3 G2 q9 E5 \8 Z( t8 ^ <div class="hello">% M# k8 l6 [: z5 x
3 T8 ~& G, u" d7 E8 _ <h2>{{this.$store.state.count}}</h2>8 K! F% {+ r" }% K, r- x
! R. X+ Y( A0 F2 Z3 A [4 a
</div>9 e( F- b# a1 W u0 x# D
</template># I! p5 b8 ?) A7 j7 o3 A$ o/ `
, O0 F% c( w. v3 D3 D) {1 r. U# a6 M9 X; n; U( V
显示结果为: 1
* d$ @" D' C" w7 G' U9 ~
# G- p F7 v- ?* D. P& O/ x6 U+ C2 _; i, Y
(4)Getter相当于vue中的computed计算属性,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算,这里我们可以通过定义vuex的Getter来获取,Getters 可以用于监听、state中的值的变化,返回计算后的结果,这里我们修改Hello World.vue文件如下:
i( e5 ~0 _1 o8 G# u5 C
7 _6 m0 _: [" D8 Y. C1 wconst store = new Vuex.Store({
: H) v K8 k# f" e4 x! K state: {/ Q# I- Q9 {6 z1 p! I% Q
count:19 e$ [5 X4 S% t/ \, M
},5 Z! K0 v9 n' q" o0 ^2 x6 V
getters:{0 G0 E6 C' x* T* B0 f
getStateCount:state=>{& X Z/ B' L: d% v: p! K
return state.count + 1;
% k0 G5 n6 E) b' v# E# R9 y }
' E4 U+ G+ y e F M& _6 u } b Y4 ^1 Y5 E- H) I
})
7 L* t6 |1 U% G9 z; {+ s. f
. V0 D( u, S z' j% C<template>- h# @2 X' F3 E0 M3 D% @
<div class="hello">- U2 S6 |+ r+ Y3 E0 @
- v- k- W) z5 p# } <h2>StateValue: {{this.$store.state.count}}</h2>
2 J' h/ c, V1 z2 x; B <h2>GettesVaule: {{this.$store.getters.getStateCount}}</h2>. `5 B- X5 T1 ^
</div>
4 e5 K3 B. ]! F% X) B! F</template>- i9 m! T+ D. Q$ @
. V) A: f6 b# [; E4 c
% z% s* [; O9 `3 u显示结果:
1 v' c$ F$ ^1 B& V+ E/ ?StateValue: 1GettesVaule: 2) r3 y5 W w0 P) B: _) Z' i- d
1 T8 c7 w, z3 f4 I/ z(5)
n. M& g. P! a/ iMutations: 数据我们在页面是获取到了,但是如果我们需要修改count值怎么办?如果需要修改store中的值唯一的方法就是提交mutation来修改,我们现在Hello World.vue文件中添加两个按钮,一个加1,一个减1;这里我们点击按钮调用addFun(执行加的方法)和reductionFun(执行减法的方法),然后在里面直接提交mutations中的方法修改值:
/ f1 K8 T' a& z; V0 o, P) f修改index.js文件,添加mutations,在mutations中定义两个函数,用来对count加1和减1,这里定义的两个方法就是上面commit提交的两个方法如下:
5 @3 `5 R4 S/ e! R4 Jmutations:{/ k& d8 k: _0 w
add(state){% ]- l5 b3 F% H( H
state.count = state.count + 1;
- m B+ c! W& G% q }," Q* g9 ^$ @: V7 x
reduce(state){
0 X8 y6 B$ c) d6 c% c' z, v7 ? state.count = state.count - 1;: H% Z* V" \0 O! ~( O) R8 n0 \
}
& s' g6 G5 [! v9 T }
9 m4 ?6 m$ r8 y* a" L<template>
5 h% ?- M* h0 p <div class="hello">2 \9 G1 n3 G1 t. K% n7 O- j9 _
<h1>Actions:</h1>
D* Z* n" w$ h7 K: t: d <el-button type="primary" @click="addFunc()">Add</el-button>
8 e7 W; y+ t% W# }: K9 v- _" V* [ <el-button type="Secondary" @click="reduceFunc()">Reduce</el-button>/ D7 ^( B- H0 F" H/ p; ?$ z2 G
<h2>StateValue: {{this.$store.state.count}}</h2>' F+ q9 c3 c( Z1 [% Y6 Z: F% A1 x
<h2>GettesVaule: {{this.$store.getters.getStateCount}}</h2>5 Q& Q! I8 ~6 M6 t0 q" e9 `& S9 E( g
</div>
( a$ T. v; T5 [9 |) F</template> m3 [( o& w0 R7 U0 N5 a8 E
. V- M3 y, x5 J& X* w, A; J4 v5 [
. \6 a1 P- m5 X5 ?结果:- z- o' ?' h5 L& o: ]0 D3 L
3 \3 a5 V- E! L' I0 M" ]: _/ q: U. h* K' Q
Actions:Add ReduceStateValue: 5GettesVaule: 61 w U( W* ^2 Z7 h- ]+ c+ m
; }' M7 ^. D$ k; i/ Z* c" c
0 h* i6 m6 C7 v! _) ]# Q W. b6 n# q2 v j& _+ w
5 P8 b3 e: `' R% q* s+ T1 P* k
( |, ^1 e) d- H$ x/ n, {2 W |
& j) C( I- `, H0 y \1 V1 O) i, z' D# ~8 c- ~1 U7 y
(6)Actions:4 j: C* ^, y. I' Z% g& k
我们看到,当点击三次后值从2变成了-1;页面上的值是改变了;我们达到了修改store中状态值的目的,但是,官方并不介意我们这样直接去修改store里面的值,而是让我们去提交一个actions,在actions中提交mutation再去修改状态值,接下来我们修改index.js文件,先定义actions提交mutation的函数: 3 a `$ w! _4 E8 E9 s7 W* r1 |& f
8 C% p. G$ E& Y: h6 V# d
mutations:{
/ e$ ^7 x! C# }! [6 p1 [) U add(state){4 V# n# Y8 y) o2 A8 x& t5 i
state.count = state.count + 1;
: j0 l7 Q6 g5 P. i9 d- K8 W9 Y8 ` },
/ b3 b9 P! R+ R" Z( T( Z% a reduce(state){
# V+ }) S0 L5 ^4 y7 J2 o state.count = state.count - 1;
" S) X& d2 f+ f& R5 w/ E( z+ j }) U' J( V7 W; U$ B5 Z6 c9 H% a
},/ Q0 R8 k8 i# _6 Q4 ]
actions:{- D4 b$ D+ \. f7 V3 m8 P
addFunc(context){2 k1 x j$ n- u; B& b
context.commit("add");0 _( P: ~* S9 G+ _ V, T
},( ]6 a, W* z! k. e% m
reduceFunc(context){
% b- o4 ]- P# W7 }; o' _ context.commit("reduce");
1 R/ ]' |; g; V8 P+ t. E% f }# t" R4 c. X9 F! M# d" ~0 J
}
% j! Q, W' E3 q h. n$ [
% q/ A* A2 z" X9 n3 j0 i% u3 i" _% t# K9 ]
3 g( C% r. L* n% L8 d methods:{
+ l1 ?9 J' @* W6 z' d* q( y% f3 d+ t addFunc(){
: ]2 W3 p+ t; N //this.$store.commit("add"); d+ c1 m( a. P; A
this.$store.dispaTCh("addFunc");
# Q! \5 J E, g1 [( u },
( x) D1 W$ y# j8 M reduceFunc(){9 z- X# ]6 M- [$ J$ x# {1 u! ~
//this.$store.commit("reduce");5 Y/ k/ x6 W! o3 O2 g% m3 ^4 R! w) h
this.$store.dispatch("reduceFunc");* J8 e% r/ _, K0 y) x. T
}. F3 ?1 v; z- G, a* _! h0 ~, {# |
}
0 t7 G+ m: D) N. r. h# m( P
9 D3 D: ^( O/ z; { w2 [这里我们把commit提交mutations修改为使用dispatch来提交actions;我们点击页面,效果是一样的) _: L/ G- E- J: g3 _- y
实现效果一样!!!!* O% b' K. ^9 I1 O/ g- x" t& B
9 ~$ U+ P' c9 ^$ _1 ? \6 h
: S1 b$ q/ ~! g5 |# \5 x2 ^% ~( c; o/ D& L9 {1 c
. c8 G: i6 W! C. r, ~$ O
1 v& H6 R. H) C( t
' H7 b7 I, X% D- d5 A
, D/ N$ H6 X0 m" G: a- H
) c+ ?) A& J* k5 d7 z; k1 N+ F, _! l' y8 G$ Z4 K o
+ i8 i! A* S: ^5 L
' D3 e9 H+ P1 p9 F$ {
2 C2 v- U" P! z: k4 Y* l- n& U; G3 n8 K1 G" Q" Y
( C R9 I$ B F0 W" i7 ^ j0 z' N% m. d4 r2 E% @
3 a" v7 g% a0 l, X, c9 w7 P
|
|