|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Vuex 的用法详解
! Y% N' @1 J$ `/ ?$ N3 Z0 g* v# J
7 d2 o, {/ S5 R2 f8 F, G$ h0 k本案例测试下Vuex的使用,创建一个新的web项目
& k& P7 ?4 n2 [3 s6 r/ sVue init webpack web
$ O2 F: y- ?& X6 A# {安装vuex
* N% y' l* J9 X x5 d3 Onpm install vuex --save/ T( j+ i9 P q( x! z
启动, npm run dev,打开localhost:8080 即可
0 L3 B* Y% v- b( w) Y8 ^. M2 A% Q# V+ W8 s( ?8 v, x
4 @" A2 z; B- P; x1 L9 f
(1)引入vuex( _0 \# W* f; I9 f6 F* E
创建一个src/store文件夹,创建index.js , 引入并导出store
# p7 e0 m' R2 Wimport Vue from 'vue'
7 j: f( }, t Z8 ?import Vuex from 'vuex'
j6 d! s" U. W1 V9 d! EVue.use(Vuex);
, w; ]# R: n! p* U. A6 U% @- h2 Econst store = new Vuex.Store({2 ]" {" E+ I0 ^/ D* r9 o4 i8 @
})
$ X) Q. k8 E: |! N+ |! E. b) F9 Fexport default store;
( W M) U8 G) e) X- A! F( d/ [7 K; G* {5 D3 ^
(2)引入store到main.js0 c# i- B* n9 j) z1 M
import Vue from 'vue'6 u6 m9 p- `+ [8 H2 A7 u4 @
import App from './App') m d& M0 f! k- D
import store from './store'! V8 ~( k/ @5 V! \) c
' G: v% A$ Z4 L, [
Vue.config.productionTip = false
2 Z. u6 x/ m8 f) {3 ~3 V
4 m8 B+ G* r4 u7 j8 q2 I( Z/* eslint-disable no-new */
, v; `2 E7 m. X0 Rnew Vue({
. e& I# E* h6 X el: '#app',! j$ }1 T. o$ S. o
store,
, j* X2 e- k+ d components: { App },9 w" w) I0 H: A N7 P4 s$ {
template: '<App/>'/ Q4 J, i* m4 v* |6 v
})
, ^+ z4 o1 A" }( R6 f, a* Z# n3 ]8 v
: _9 n! L5 ]( U7 |& C9 [' L' F
7 u( t$ P0 r) L* N(3) 修改 helloword.vue ,先使用state获取值进行测试
7 S A9 T5 H; n1 J- i1 V' zState: vuex中的数据源,我们需要保存的数据就保存在这里,可以在页面通过 this.$store.state来获取我们定义的数据;
, M/ r( l) g; ^4 i1 k0 A) {' Qconst store = new Vuex.Store({) Y' ^) |: z1 ^. c; x. d( J2 |
state: {* v* c8 h4 r- y( J- @
count:1
% X: L; _3 ^( T }
( _4 B8 {+ l$ }. u1 ?; M/ ]% t2 j})) U1 b& Q; \% M( Z) @+ _7 |, v
% ` O) z/ F0 P6 A2 J) X0 i, A4 c2 ]helloword.vue----% |5 `+ u& u8 m u' ~3 U
<template>+ s2 @6 c' C$ O
<div class="hello">
% ^6 Z' q1 k5 `) |% K- |
" F& K, a( W# P" Z# F <h2>{{this.$store.state.count}}</h2>
& E, X; X* w6 N7 B7 E' P P' w3 s" _- O R/ i
</div>" M& E( L/ X* e
</template>8 e. Q) u" h; ]. b1 C( N
* e, p7 H: B! w7 K
0 B, r' t- k' Y! o* y显示结果为: 1 : I5 Z6 N" E8 Y4 F5 a1 d
, X) l8 p& l- R" b0 t+ X' ?
3 q; j, P# Z" S(4)Getter相当于vue中的computed计算属性,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算,这里我们可以通过定义vuex的Getter来获取,Getters 可以用于监听、state中的值的变化,返回计算后的结果,这里我们修改Hello World.vue文件如下:
& v* z, j I. h- d. Z. A
4 D8 B; w8 D5 [7 F/ F- oconst store = new Vuex.Store({
5 v) k* P& z p* Q9 v) W% x state: {
+ ]7 X3 R; u2 R9 m count:1
2 V2 S2 B3 n9 W9 Y },
: `( W( u) w5 R" B- q getters:{
Y4 ?) z4 W3 H6 }% B) s getStateCount:state=>{
4 x, C5 r" ~1 p+ O! X return state.count + 1;
% m. V6 F( g+ m+ T6 I9 @ }
9 V5 T+ f: l) q }7 _3 n( |9 [5 @- o8 R# [
})
' L6 C. f* _, f! Z: a7 F5 d+ K! a' \$ G9 s! W) |
<template>
9 V ]# }( `; h <div class="hello">: T7 B$ ^2 t' g4 o$ R$ Z
7 r/ n: ^( B9 p% u8 p <h2>StateValue: {{this.$store.state.count}}</h2>
% b# F9 Y# C1 b% D' } <h2>GettesVaule: {{this.$store.getters.getStateCount}}</h2>0 Q9 d7 `" d: D5 u3 v* N
</div>
3 B7 S& Y) H6 `9 f! ^</template>/ R% R2 Z" J* m& @
$ s& N u/ R6 {( |
! X) p# l4 y, Y' H6 B) k& m显示结果:; c. l9 {" ~+ K
StateValue: 1GettesVaule: 2" C+ z9 Q" J+ ^4 K; c) z
8 ?2 m+ h7 a0 q8 W(5)
) `8 K9 [, K. P* }Mutations: 数据我们在页面是获取到了,但是如果我们需要修改count值怎么办?如果需要修改store中的值唯一的方法就是提交mutation来修改,我们现在Hello World.vue文件中添加两个按钮,一个加1,一个减1;这里我们点击按钮调用addFun(执行加的方法)和reductionFun(执行减法的方法),然后在里面直接提交mutations中的方法修改值:
/ J# X, ^. j+ S& l$ o* ?2 t4 H( s修改index.js文件,添加mutations,在mutations中定义两个函数,用来对count加1和减1,这里定义的两个方法就是上面commit提交的两个方法如下:- @+ T3 q, C5 ~( L9 ]- v: P, H
mutations:{
; u! w/ }# c$ V4 |' j. ~% R* B add(state){
. i3 m! m+ X B5 }4 y. f state.count = state.count + 1;
$ V, Y2 b g& m( j$ {3 i },& m2 h0 l* E& G5 f2 T$ M% G5 @
reduce(state){
& t F: ^: p( a; @/ p2 l state.count = state.count - 1;( V5 M- [8 N, b% Y% T
}$ F& R! Q: Q A) [3 p3 b8 ?' M) k
}
6 S2 \$ U$ u9 c( f; O/ t<template>
: L2 l" X0 H& ~* w& W7 r/ X <div class="hello">
. P4 K4 T" F4 a8 N3 c5 X2 E W7 q <h1>Actions:</h1> q. B4 u9 _# C
<el-button type="primary" @click="addFunc()">Add</el-button>; \( R( n$ B( ]
<el-button type="Secondary" @click="reduceFunc()">Reduce</el-button>5 Y9 H( h3 x" o
<h2>StateValue: {{this.$store.state.count}}</h2>
% `- a: ]) l# r0 y8 ~" W6 F5 g" [8 N1 T <h2>GettesVaule: {{this.$store.getters.getStateCount}}</h2>$ y$ o; a* z9 o! f7 p7 J9 |) u7 D
</div>( c# o4 z- ?( u" N U% D, o
</template>. |( l3 A/ ?, H* ~: Z( {8 i, `( H, g
% p% ~9 A4 M. U7 G1 {$ P
j% l- z: z. |! C7 J- h& x2 s j* m
结果:
8 I" E9 R2 E9 [0 m0 H7 r( N, E8 x" o4 V9 Z
+ o$ l. T! c4 c
Actions:Add ReduceStateValue: 5GettesVaule: 6
- V) |8 ]. p$ w2 W
0 w: V6 w3 k) q. ]3 l7 S" _
# G3 n- p: L* b/ I. d
4 w: v* Y1 ~: z4 I; V- s! U- R
- [" @9 R; H- q/ Y$ p1 J6 ?. L1 J" Q: x: O
4 K# z! T5 Q( ~* M/ w# B2 m0 |0 _
* w( j$ t9 l2 t/ Z3 S8 L) g+ r3 ^3 @
; P7 ]; c X$ R! c; x(6)Actions:0 }3 O" z7 B" F6 _
我们看到,当点击三次后值从2变成了-1;页面上的值是改变了;我们达到了修改store中状态值的目的,但是,官方并不介意我们这样直接去修改store里面的值,而是让我们去提交一个actions,在actions中提交mutation再去修改状态值,接下来我们修改index.js文件,先定义actions提交mutation的函数:
8 D7 Y( n7 H& p' ]& t' y1 G * E9 t- f/ Y9 F$ `7 Y' `
mutations:{
" z4 `" k- g2 B4 {/ t" B; N% w3 K add(state){9 V9 Y# X; y+ e: g7 g8 a& o
state.count = state.count + 1;" @4 i0 G* t5 z3 J9 t/ t- u a% \
},
5 N1 J3 t& @6 P2 y C, s0 F reduce(state){
9 W/ \& }* M( J( {% |' @ state.count = state.count - 1;+ f- `9 ?, ~8 A8 u% ?; P
}
' R2 ~( d) t: X6 }( a! K4 s! F },
$ d; j8 ^/ q) h actions:{9 `6 O6 n/ K/ Y0 E. I- h6 h% ~0 a
addFunc(context){
6 E* _, w% Y& o context.commit("add");
& r: f; \/ k4 c1 ~. d( S( e. o2 b },
) m9 K e" G1 q3 M" U reduceFunc(context){) |6 t7 G/ y' J6 U
context.commit("reduce");
/ H3 l1 w$ O0 O; `/ g# T) S# G* V2 i }
5 W: g$ y! W# P2 c, ~# A }
# {% k1 _- X. C% X' W! v
( l8 A& ?$ z) N# J; X: @8 D h) C
/ i N( j- S5 z+ ? / X1 t7 X" ~( A1 W' B1 d
methods:{
1 w; f9 _& ^5 c) w! j$ H9 M4 y addFunc(){
- X6 D8 _0 X( a W) H P( o //this.$store.commit("add");
. q5 V1 R7 z4 Z* g) q7 m* t this.$store.dispaTCh("addFunc");! E5 S, z$ d$ j" ~' w' B
}," N6 T3 i5 Q: k7 i" M
reduceFunc(){
8 p9 y3 U( ?! @# i0 Y" v# a; Z //this.$store.commit("reduce");! c2 E/ }2 K) z! n; }. n
this.$store.dispatch("reduceFunc");8 D' b* I0 k+ E4 N" r; P3 B s/ A
}
1 R+ _: b0 b" @# a8 _# j }
) L& x. f" E$ B* l( k8 [6 i
/ Z9 Q( W, L/ Y/ u这里我们把commit提交mutations修改为使用dispatch来提交actions;我们点击页面,效果是一样的
+ E" J2 M7 v. \, a( I: |$ O实现效果一样!!!!
/ V+ K- p7 t, C5 W- ?, S
9 b/ C$ a# j# [5 I: A4 n' a# f7 N8 r
) O/ |1 V7 y+ Q' s
1 |% x5 r" U* B) j3 P, w- N) K+ u4 L7 p9 `4 @3 s' h+ u
0 C/ {* G$ Z1 J& M
- w( D j+ ^0 ~( A: G4 `
( t+ Z2 Z$ ~' j0 P' Y) o' z' s+ V1 s3 H) V
5 L, Z% o' B) q, b8 M) }: [
' E4 M9 G* }! F& Z. n9 o; F, m: O5 s5 s9 m, Q7 k
2 D0 b/ @: [8 `+ U: H0 B) E8 n" [0 @6 k' C$ h* q9 U
- G$ l& d( N6 K" U$ ]( _' o; x# F' J
( W/ i0 o, K4 w" m" y: C9 \ |
|