|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Vuex 的用法详解 {0 |) p3 m `0 S$ V" V% U
; ]0 M* z! B0 M本案例测试下Vuex的使用,创建一个新的web项目0 ~2 m" p' ]/ i
Vue init webpack web2 H8 Y* u$ l4 _+ Q+ Z0 {
安装vuex1 l. t: @; d: m! H1 R9 S* D- D
npm install vuex --save/ F' Z. ^* |& z; p3 e' U- \, a
启动, npm run dev,打开localhost:8080 即可
; Q3 n* F1 `& z9 b( @# f( } i! c0 Y1 |1 O8 m+ K0 A
+ v( i4 F- N: Z5 H7 [
(1)引入vuex7 }0 }" o( ^9 q/ Z4 }3 Z5 x
创建一个src/store文件夹,创建index.js , 引入并导出store1 O# d) L0 v+ c0 L. J1 s; O0 s
import Vue from 'vue'
L0 z! y( c: N4 i9 D0 ximport Vuex from 'vuex'4 _! G3 ^, n* J+ a2 l- m9 e
Vue.use(Vuex);
5 V' y z7 q: h% M% J Zconst store = new Vuex.Store({
# o! X' ?4 t+ D/ v6 r})) l* }8 n0 \4 A. |! T! s2 p& b2 q8 q: u
export default store;" s' S9 g- a4 }( v! j' N7 h% D
) X! q/ O, q5 D. d* h* o* t(2)引入store到main.js
0 b( J3 P: C0 z( D& r9 e% Q( |import Vue from 'vue'; [; e; i7 g$ U' {
import App from './App'
5 _+ b/ \8 C" aimport store from './store'
1 Q4 [# A8 Y# M. `4 U& w8 T1 g. f
8 J* ]3 i4 t/ N% N9 e) S1 fVue.config.productionTip = false
2 z/ Z5 G+ b. F3 {+ [, @1 R( c+ [) Z3 K% }
/* eslint-disable no-new */
3 j4 A: P6 G6 z# ?/ e8 S! ]# enew Vue({& `2 |4 S$ I, @# G, n, ^* {
el: '#app',
6 \, l3 `* Z" S( y' |6 l store,+ L3 a. c9 _3 A, q, j- @! M
components: { App },0 t( A R7 P5 h9 \3 s+ A* Q* |* [
template: '<App/>'* U6 f: \& o# ?7 r: l' y! O
})
' @0 Q6 \% N/ r8 M0 N, `0 q$ r e& W2 b( K; N
3 F* R0 V" j/ N4 d1 |) c K0 d: y. Z$ \1 F8 _- C5 j7 T( [6 c
(3) 修改 helloword.vue ,先使用state获取值进行测试
' `8 g. [) L5 O/ dState: vuex中的数据源,我们需要保存的数据就保存在这里,可以在页面通过 this.$store.state来获取我们定义的数据;
7 E; o( U! j( C! ?2 d# H+ jconst store = new Vuex.Store({% J5 T) B% t' ?" c! `
state: {
) V/ |9 Z% g' F% B9 |' J+ z count:1
O) a3 ~' l3 x- c a& j }
& N1 E* W3 s# X$ r) Z9 M/ h})$ \1 k: F9 w- a8 M9 ? x0 Q
: L2 L/ j& q7 V& S0 K
helloword.vue----5 b+ v; l Q$ u" d% L
<template>) c, { l7 V2 ^( E. l! E6 e7 m
<div class="hello">
- G/ l/ w6 f/ S9 u5 c2 N; ^6 ^, K E: c
<h2>{{this.$store.state.count}}</h2>
9 W: A9 h5 @& f. U- I# K& i
1 \- ~- a: ~3 D! u </div>
5 l4 l' z( l, C' p) g</template>
, Y- n0 G+ K& @& q# Y0 Z& o9 s! O' M5 @) g
2 w+ G$ m6 q/ V
显示结果为: 1
$ ]# T# z* v3 `5 a. ~ r: h+ s7 L" ~! W/ v
) q% y/ K: f6 l; T(4)Getter相当于vue中的computed计算属性,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算,这里我们可以通过定义vuex的Getter来获取,Getters 可以用于监听、state中的值的变化,返回计算后的结果,这里我们修改Hello World.vue文件如下:2 ]& k3 ^* i2 `) K( @$ L+ k. B
5 i2 g( V" f( a* d7 f* pconst store = new Vuex.Store({
' j0 B$ y" U0 U5 q state: {
5 }8 v- V, q7 {- K8 Q/ M count:1 [5 T5 j. D1 _% c
},
u; w2 X5 X9 M$ H, [: x3 z! }; ] ]" W getters:{+ h8 ]! e6 t; E/ z+ x: A# ~* s
getStateCount:state=>{
1 G; D6 Z% E; E( J. k return state.count + 1;
# o* |; W9 q3 F* M- e! e }2 L8 L2 P( B6 R Z. [
}. S; U: u! z: S; H! F( m+ ~7 M, A
})
p v2 C; Q; M3 g1 ~ T" I V
% ~2 \# o- H! U: ^9 z<template>6 K9 \, s% N' [6 D: o
<div class="hello">
; S8 J& v4 Y- \( W" f7 u( I1 m: e' ]8 w3 g, o% p) ]
<h2>StateValue: {{this.$store.state.count}}</h2>* v% h+ f0 ~4 ]( d; C* T: w1 R8 e
<h2>GettesVaule: {{this.$store.getters.getStateCount}}</h2>- c" f- v9 `4 {9 }8 m
</div>5 ]) l. h7 ?( |& ]- D
</template>
/ T: T) G8 Z& k/ h
7 a/ E0 p% l' t1 g8 ~1 V( D7 R% s! w. H
显示结果: {" K9 `3 c2 E5 j) k& d! S5 k
StateValue: 1GettesVaule: 2! g$ W4 A* \" u% v& ?8 y$ g
: B- l) w- O& t' g1 B: N% V6 h; r
(5)! _/ S! C2 T/ f# n6 i2 U8 _4 }
Mutations: 数据我们在页面是获取到了,但是如果我们需要修改count值怎么办?如果需要修改store中的值唯一的方法就是提交mutation来修改,我们现在Hello World.vue文件中添加两个按钮,一个加1,一个减1;这里我们点击按钮调用addFun(执行加的方法)和reductionFun(执行减法的方法),然后在里面直接提交mutations中的方法修改值:
% r3 r" T' ]4 B8 I, Y0 l0 `: Z! V修改index.js文件,添加mutations,在mutations中定义两个函数,用来对count加1和减1,这里定义的两个方法就是上面commit提交的两个方法如下:& j m) n" ^3 v1 B
mutations:{
7 j* l% |& D% i. J3 J add(state){; H% Y* H; i( X) `3 x
state.count = state.count + 1;! ?; M2 M& v, D% N; @3 Y
},
) x$ U; Y8 z% v0 s reduce(state){* }$ r' s; Y( `. @) ^8 N r4 d
state.count = state.count - 1;. ~: s$ r' W8 Q1 n$ b0 @7 I9 n
}, c) Y- [/ R7 g9 d5 \& m$ D3 M+ C
}
8 d" H4 V2 W9 c+ r<template>
$ F/ j2 v$ D, i: N' b <div class="hello">) c& s$ a8 [. G0 _- ?
<h1>Actions:</h1>
1 o9 g1 d8 n, F) c9 V <el-button type="primary" @click="addFunc()">Add</el-button>. R# c3 E3 G8 m, Y; _. n
<el-button type="Secondary" @click="reduceFunc()">Reduce</el-button>% ^- J7 k1 M8 n# ~: l. @0 M3 @* U" V2 [
<h2>StateValue: {{this.$store.state.count}}</h2>
% I" w3 p! L! D4 R0 L/ p& l% u <h2>GettesVaule: {{this.$store.getters.getStateCount}}</h2>. \# F- Q$ `7 _3 v+ y M
</div>5 B* I: P! d; O! y% G3 r/ E: J
</template>
3 ]+ s( {3 G( |+ w
- |% O# y* s3 A9 v$ F% r0 P; {3 | E' Z8 b
结果:, S# Y) {$ x+ K0 h! `6 b
- Y( b* T+ t3 r- s( b4 f- U: H; W8 F. S. K- j1 b
Actions:Add ReduceStateValue: 5GettesVaule: 6
7 `" I2 Z# a; y) `% J F
% h) [$ f, G% l0 L( S: m
, z4 e& q4 I. @) Q4 Y* }; u% {
e# G; w0 z! }: B% ]
6 q7 }8 y# V, ?# m4 A: ]. P/ P# D% K W
8 Z9 W) R( Y) O
% s6 Z6 H2 C4 _ f m
; b) Q6 D8 v! q) q( o(6)Actions:& b6 h* v( Z8 {
我们看到,当点击三次后值从2变成了-1;页面上的值是改变了;我们达到了修改store中状态值的目的,但是,官方并不介意我们这样直接去修改store里面的值,而是让我们去提交一个actions,在actions中提交mutation再去修改状态值,接下来我们修改index.js文件,先定义actions提交mutation的函数: ! Z* R1 p' d2 `! y- k
1 F% ^' p* n: ~0 W4 u7 K
mutations:{
* P/ a8 j: [' D, { add(state){1 k" y5 t/ C* q r# Z: F; o9 h6 P
state.count = state.count + 1;
7 t5 x; \" S9 l9 n },9 S& c( ?. G5 z: G0 ~- F0 r5 \
reduce(state){
2 u/ E: w* m: V; H, A" \5 W1 g( C state.count = state.count - 1;. i' M7 Q. J9 M# c; \- h
}
6 R P6 q! G' ]: m }," L5 g. L, b* E5 y5 f
actions:{
5 ^: K! K# f& l$ c; E addFunc(context){
# I! S! d, E4 _1 } context.commit("add");+ ^4 a' m, \" t
},+ s: F- _ ?3 ~3 x* D. ]: o
reduceFunc(context){
! @) ^2 ~- _9 c$ \$ L: X context.commit("reduce");4 J9 k6 \" b1 k4 m5 s! |
}
4 F. h5 }% L# h }
* k1 J# v X+ p3 |- Y; l. M: s! [; s) l
: K6 x1 [& J1 a; |0 ~8 R4 f) ]1 n
) r: Q S ?. T8 q% H' n3 C) ~
methods:{
% m$ a7 a! N3 E; @# r0 B( O addFunc(){9 F3 L9 Q8 M' ^) e* j4 t5 Z. y
//this.$store.commit("add");
8 @; i) M& Z" J: w7 a- B this.$store.dispaTCh("addFunc");
$ J3 D- J* a! X. f: s+ `, b },
5 _% j+ z" j. v; w! o: K. i reduceFunc(){
5 B6 A6 f8 \ {2 ? //this.$store.commit("reduce"); N" h! T# J, x( @' P+ n# r$ v* E
this.$store.dispatch("reduceFunc");
* S. A+ D/ ^% J. f; e! j7 s }+ g( |+ `$ L% j+ o" b$ B
} j: b1 d a: |, Y" h" a% S! i+ S6 }
* m# J/ s; l- |2 ^这里我们把commit提交mutations修改为使用dispatch来提交actions;我们点击页面,效果是一样的( |# C2 Q t: v4 G
实现效果一样!!!!
/ ~' D2 Y) G+ ~$ S: c1 x9 @) Z# `
N, W* J5 k7 y" N8 i+ l2 u* {* ^. S6 h0 c$ ~. }6 F0 H0 K8 \
% Z2 F X9 @) }% K
8 }3 ~) L) p5 r
+ @, R* j) O, V% n4 c0 y' w
4 O) y7 f% g3 Q; ]3 q. m
, r' y3 j9 H' H; l! q: i, Q: N
0 h3 g9 s& b# Y8 `. ^9 `+ l7 F% O( Y* w0 U# Y; p) } ~; i
, L/ Y" ? w6 ]
0 T! B$ L. i5 n8 F' k, F1 x% ^7 _- G4 x: p9 o
4 f% P5 ~2 \8 _, z F
( {# W+ i( q# N0 v- l) _
0 X1 J: V( L I' o" f
' N% y+ E- l! _5 L |
|