|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Vuex 的用法详解3 b, b0 i) P. h- P/ X
W7 p, G4 Z) I" x
本案例测试下Vuex的使用,创建一个新的web项目
! X" Z" _- R& S z5 k$ d" |$ DVue init webpack web
4 M/ ?- v; `9 g" l/ R' B9 _6 I. V安装vuex
0 M. x+ R9 ]3 rnpm install vuex --save
2 c1 \/ D8 P* m0 N0 \2 S% b* j启动, npm run dev,打开localhost:8080 即可
6 @$ H0 w& Z8 y' u# I$ Q% T2 w' _2 r# `/ S( e1 h9 b- F3 H
& U3 n. i$ A! s) x% [# u) w t
(1)引入vuex
& a/ _0 y6 ]% L. e1 f6 g) P' j创建一个src/store文件夹,创建index.js , 引入并导出store
6 z2 s# b M/ Z4 s6 f) Nimport Vue from 'vue'
# M1 U4 m q2 x, fimport Vuex from 'vuex'
! e( o7 J* w5 rVue.use(Vuex);
$ y+ k; H! n' Hconst store = new Vuex.Store({
6 o0 R; e e2 p9 k. n})' o, x/ `4 c- [/ O7 f
export default store;) S, U1 {5 o" Q. x/ B5 V
+ N8 _# ^( V& v9 Z$ V5 [6 [
(2)引入store到main.js, X1 v d9 E. d7 V* ]
import Vue from 'vue'
z7 s7 H7 s- x$ S( Kimport App from './App'
" \: h' j9 s- ]2 Eimport store from './store'& F# y/ i( w r1 u$ ]
7 P, _; n2 r0 q- v6 n: MVue.config.productionTip = false
; ]; q0 d, `1 Y( l; m0 s, `8 e2 u8 |' X$ S' _# S
/* eslint-disable no-new */
3 V/ a1 N0 s" S- Y0 a3 S9 o" Wnew Vue({& X$ i1 b/ X7 _! F4 r' g
el: '#app',$ U7 C ]4 G; b" ]
store,& y& {) V+ A% G/ H! o; _" N" X& k
components: { App },5 Q# ]% y5 J) `: y; Q; t. T
template: '<App/>'
9 r V0 {. M, c1 u})
9 X9 j! A. J6 M" p( R) O: t
+ i7 g. t2 O% U6 n. f2 G) L% P+ M3 z, R, s
0 T$ B0 D4 Y1 r2 _1 H& \9 h
(3) 修改 helloword.vue ,先使用state获取值进行测试2 ]( S3 b6 r+ q Z$ c7 R1 E
State: vuex中的数据源,我们需要保存的数据就保存在这里,可以在页面通过 this.$store.state来获取我们定义的数据;
4 `3 |1 B8 m, v& C. {: |$ t# Aconst store = new Vuex.Store({0 Z. S8 V2 k- Y+ c6 ?. d
state: {# {: V4 o/ h1 s4 r3 P
count:1' A" q/ ^9 p4 c! _! }
}
1 ^1 w0 j' V7 D$ o0 u3 C}); r* F$ Y }) W
5 H H. M T1 Y4 s
helloword.vue----
, Y: S* z. a3 d9 S<template>% H8 \# O3 ?& L7 y0 u; h
<div class="hello"> o$ U! `- m0 n' I& d! H5 N' R
" k5 X+ n3 x) p9 Y. x4 D
<h2>{{this.$store.state.count}}</h2>
" E% h' Q* B7 R4 {: Y
) I9 g( m3 O; e& ]4 W. X6 v </div>
/ K+ V$ {0 n3 q8 ~. @, e</template>0 x2 u6 c+ P9 ~3 Q/ B6 Y. J" |; ?
+ b, M4 Q/ I) b( q H0 I5 q* ^) i- }2 ~) [# A- [* R
显示结果为: 1 ; W6 t! @' s. m
2 V5 F; t; j! Z: s
% N, ~% o8 y+ v1 E% m
(4)Getter相当于vue中的computed计算属性,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算,这里我们可以通过定义vuex的Getter来获取,Getters 可以用于监听、state中的值的变化,返回计算后的结果,这里我们修改Hello World.vue文件如下:' p4 c* @( d8 y" N" c" E' d
. t0 n9 M! ?5 H! f4 |const store = new Vuex.Store({2 ~5 Z/ E! }. Z' l5 R& o. t0 U
state: {+ h# r3 i r8 f. D$ E$ X0 \. [
count:1
" L' f& T$ c) @/ G9 h2 L4 c) v },8 U0 E& h4 h8 _( h- S% ]8 V( E
getters:{
5 k1 Y6 [" F( j getStateCount:state=>{1 {5 @! H6 @2 v l$ c# L/ E9 Q
return state.count + 1;
9 p ~) g* N& _/ b }9 o( I! F$ |. U6 m3 Z$ ?
}$ D, f1 g# _+ X9 W# I# Q
})
2 c# W d* ` J4 }$ s, f' o. N& p2 s# `/ `6 ^# ]2 P* c G! O6 W
<template>* @; f+ e% m7 }5 b- B# Q
<div class="hello">; P: I7 }# r; F5 e
: s8 k! K9 U9 I6 Q% j' z3 x <h2>StateValue: {{this.$store.state.count}}</h2>4 u3 G( O6 j! L3 ?
<h2>GettesVaule: {{this.$store.getters.getStateCount}}</h2>
. A3 v# n' q- i, W </div>
2 z# I' d4 [5 b8 B- C6 _% Y</template>3 C: t3 N2 d: _- [. y
3 S0 P5 ?, h7 c' L( c2 y _7 H
. I) N/ \$ q1 ^4 R显示结果:
) y$ C W% ]5 k6 r) A. MStateValue: 1GettesVaule: 2
4 V8 m# o& n. E9 r |- f
+ @5 c% g: h4 ?* K$ v(5)
7 M1 y; d$ a& c" ^4 d# wMutations: 数据我们在页面是获取到了,但是如果我们需要修改count值怎么办?如果需要修改store中的值唯一的方法就是提交mutation来修改,我们现在Hello World.vue文件中添加两个按钮,一个加1,一个减1;这里我们点击按钮调用addFun(执行加的方法)和reductionFun(执行减法的方法),然后在里面直接提交mutations中的方法修改值: + V/ B2 a* q' I2 G3 F
修改index.js文件,添加mutations,在mutations中定义两个函数,用来对count加1和减1,这里定义的两个方法就是上面commit提交的两个方法如下:5 `8 Y* [" G/ Y0 k
mutations:{ I8 N! V# v% `" T; M0 u
add(state){
* Y5 F0 R2 k& b6 n6 D state.count = state.count + 1;7 V9 ]% {6 Y3 y# n' o/ C. c
},* f2 I0 t, a1 K' q* S+ s) z
reduce(state){- W ]3 S; a, j# c# }
state.count = state.count - 1;
& x" J8 u& H2 Y2 r5 Q/ f- R }
8 F* ~! O n( P5 | }
! q% P; R/ O _8 u: o: L<template>* r) y7 g7 w, I% P+ q1 d
<div class="hello">6 \; O# ]' k# J8 L8 a
<h1>Actions:</h1>
( L4 n- D1 B: ? a <el-button type="primary" @click="addFunc()">Add</el-button>2 I6 ]9 o. J& n. Q- N& z9 v: x
<el-button type="Secondary" @click="reduceFunc()">Reduce</el-button>
1 F; k+ o- r' k <h2>StateValue: {{this.$store.state.count}}</h2>, P0 e# o/ C* v' V) ?0 W$ q
<h2>GettesVaule: {{this.$store.getters.getStateCount}}</h2>
5 o. @. m' r7 B; { </div>3 [; \% N0 J) c- v9 s
</template>) d1 Z1 @7 I' Y! h9 @* `$ I) Q1 H
3 T8 w: ~7 H- V! R) I- G
8 v" O9 H3 H, X- z) Q2 J% c结果:
6 g9 ^# b9 I: T- E% `: R: N) ]7 s+ F
3 q5 z# R$ M* X; I2 }% d2 |1 c7 Y4 m7 u& O% {
Actions:Add ReduceStateValue: 5GettesVaule: 6
, y, p) Q: E$ M4 `6 U2 {# S8 N$ Q" q$ N8 f) Y9 n& V7 p
& h4 s* W$ Z; K6 s0 b3 h
% S# J6 A9 Q* p. @3 I/ r! D
, T ]4 E" N, j$ b, U9 o2 L" U* _3 r8 I8 R4 u) F, @) ^
% g( C* b9 U; C& \' M# B* x& t: g( ~ S
! {- X1 u& @ N+ F+ X
(6)Actions:
6 D5 x) f5 I, d6 B, A* X我们看到,当点击三次后值从2变成了-1;页面上的值是改变了;我们达到了修改store中状态值的目的,但是,官方并不介意我们这样直接去修改store里面的值,而是让我们去提交一个actions,在actions中提交mutation再去修改状态值,接下来我们修改index.js文件,先定义actions提交mutation的函数:
! A9 H3 K# i9 t; ^( M1 C7 x( E& L
; ^# i* l0 H$ b9 h) _ Imutations:{
6 l( Q" m* @& Z7 X# z add(state){
8 ~3 f1 ~1 k# ~$ s state.count = state.count + 1;+ }6 b8 }! ~7 X$ p3 j# J2 P
},
, v* v# B# F' {( b* a. v9 W reduce(state){- R8 a2 e% I- s
state.count = state.count - 1;
2 W4 A- U! d/ b7 O7 S }
' P/ ~ E; G! h9 z! m6 r& w/ V' ~ },
* ^, X0 e+ i" ?4 Y actions:{
$ J( ]! E# U5 H& o- I% t( t addFunc(context){
2 ]- o( b7 ~$ G. t1 _7 j: ^ context.commit("add");
' W9 g8 @5 f" ~: J3 L },
8 p6 d1 o; B m' V+ g( l. \ reduceFunc(context){
; S( d7 L. J% W+ `' t4 o context.commit("reduce");) N P' d7 j, C1 q
}
& [ o z2 g) ~2 p9 \4 M! H& G }+ J. W# Z# R" K8 b9 g
1 i2 e; y9 \; [% x
1 J, [8 ^* m" w: V F
+ H6 Z- C" J8 {4 r+ F; ^
methods:{( |) J) z7 n0 z: f$ ^2 t
addFunc(){
8 g; u; G! m+ I! u3 x3 A- n! W4 Y //this.$store.commit("add");
) B. z$ _) \, t9 a% s this.$store.dispaTCh("addFunc");
- ^' d: S5 L6 m* e& J5 g c },* W- ~) I% H' `& M$ o/ A. w
reduceFunc(){
1 ]8 ~1 f4 N5 w, e/ Z2 k //this.$store.commit("reduce");8 s7 ?- C& O7 @9 r
this.$store.dispatch("reduceFunc");
+ u& M% w, ]# v9 a! ?% O" Z }5 |5 G( Y6 A& v' s/ \
}& P5 |* W5 ~3 T( z
. N0 c0 P1 }" G, A, M
这里我们把commit提交mutations修改为使用dispatch来提交actions;我们点击页面,效果是一样的
$ s- O% v* V( S实现效果一样!!!!
* c+ [! o5 `0 R7 \
" a0 O# r' P& }: p2 \ z
6 x7 o7 @! W! ?* x/ v, q7 P: k* L; v3 X e
8 x q" c1 Z2 D+ T2 }- ]6 ?1 j& ]2 x$ c7 N
7 l# ^+ b% h$ u! g8 G / r% w/ l) \+ ?% y+ x
+ f! u% O$ Y8 ^1 i* \3 Z; B* W9 g" ~
+ k; k$ J" c$ q' P* b- P& i& i
8 }1 X* g6 V( C4 X8 A) ]( T# B
: m1 i8 K1 L4 ]
2 f/ @% s1 A/ ^' i; T- s! e# j( J8 D' l4 T4 [; H
" [9 r+ I/ b+ ^. C0 e- s' `: x7 E5 E& H) r6 V5 h5 w7 N P4 u, g/ N
! B' s8 J1 M6 X# f
0 `: U p: @5 k( Q |
|