|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Vuex 的用法详解) _* }5 J3 f8 h( o9 ~5 V# T8 R
0 e7 d' s: D8 e* L+ v" F本案例测试下Vuex的使用,创建一个新的web项目
3 r" i8 }. R+ E! S) ]Vue init webpack web+ K7 h( f) F# i4 y9 P }
安装vuex
( b# l& C3 k5 D8 T6 `) q" Fnpm install vuex --save& x7 B% Q7 x Z* N
启动, npm run dev,打开localhost:8080 即可
. c/ r% E/ a& T& X' X0 L& r7 @, R7 K; Y3 v/ @' _% q$ q
: v6 J1 j' U. _1 m(1)引入vuex
2 g5 N' X) c0 i$ I6 F创建一个src/store文件夹,创建index.js , 引入并导出store
6 u% v0 \- L A7 S# `import Vue from 'vue'
8 X% c7 y$ T% U+ j8 t' ^, _import Vuex from 'vuex'0 r7 ^2 {/ w% w& a* [
Vue.use(Vuex);
0 X4 v* w$ w* ?3 A3 dconst store = new Vuex.Store({
, i/ [" M, l! i; s3 r})
$ K$ O4 i$ m# sexport default store;
$ X9 Z" C, W: ]! s
/ h2 S. G! n* \8 e, h+ G(2)引入store到main.js6 c% F0 T0 t. G0 Y6 _1 q: Z
import Vue from 'vue'
* l8 R9 l( {) h5 rimport App from './App'* d# I0 a$ L" f& X% \. U- w6 y" {# s
import store from './store'5 H9 E' C; @+ ?4 W
1 v1 O. A% B" l9 v/ B" Y# D7 [$ h
Vue.config.productionTip = false u. l, d5 y" ^: x7 P
& [/ D5 G7 K1 {8 H L% _
/* eslint-disable no-new */
! c' V/ D- L s& ^0 U4 Mnew Vue({
3 ?1 {. X+ V X$ k el: '#app',. {$ b% R! F( N+ _6 P$ s8 a
store,
+ W: A4 \* Z. v+ S components: { App },2 q0 C8 N9 }. d* X+ T" e! b
template: '<App/>'0 Y. o. Z5 @" X8 Y, N' x
}) U& I0 d/ u, E# A- W
. G4 S `+ K. q, W1 [3 I
6 J, `7 g: Y4 H+ H0 n
9 c3 Z/ }$ e7 H2 S0 L0 Z5 M(3) 修改 helloword.vue ,先使用state获取值进行测试
! h/ \0 j. D$ e% d9 `% kState: vuex中的数据源,我们需要保存的数据就保存在这里,可以在页面通过 this.$store.state来获取我们定义的数据;
. e3 C7 y6 s0 s" ?8 \0 J! aconst store = new Vuex.Store({' d# m |4 V2 }! R, I/ _% _
state: {, c: A$ P/ p' ]5 v8 x- P
count:19 _. W/ u' C# j9 W: {
}/ \; u3 }2 R1 t( m' [- G8 m
})/ F; b4 X* T M, {
. N5 E- ^+ C4 p& r# l
helloword.vue----
: M+ K8 x1 ]4 u4 q<template>
* ?7 j v7 U- \! j a <div class="hello">
3 r: d! _( [; k( Z; J& m
% S. F9 Z1 a3 X* _$ o! {' y <h2>{{this.$store.state.count}}</h2>
" N& t- I: |+ ?* p0 p
7 Y$ T" U \1 N* ]: p8 ` </div>
2 b% Q# V h: L3 Z6 y5 M& K7 l! }</template>7 A5 ` P% D# Y9 J
: u! H+ W, u+ {: S' J7 W1 Z
5 a! e0 \9 G# I& y" i显示结果为: 1
, R" ? ~) Z: J8 M4 m3 I' k) ~; R$ R- h3 s
; y# S6 o+ m( T* F
(4)Getter相当于vue中的computed计算属性,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算,这里我们可以通过定义vuex的Getter来获取,Getters 可以用于监听、state中的值的变化,返回计算后的结果,这里我们修改Hello World.vue文件如下:: |( J1 Q0 s; o2 r* J3 M+ s) F
& B' c6 B+ ^0 f( b# w2 ]9 G5 n- aconst store = new Vuex.Store({
' g- ?0 U$ C% J* X2 h- ~ state: {
% v/ k( s( }9 M5 s count:1
% a& s8 t8 c" K v! g },
3 r& u) C# _. |5 B" o! A getters:{
1 K4 D5 \; q+ A/ c; a' [ getStateCount:state=>{! a7 {, N( G: l, ?
return state.count + 1;
. V8 G* `; h2 A3 \ S }. ?) c; x( c* R! J
}& o6 t, T: r8 l
})
6 q$ p% o- d* {5 F8 [) W
9 x( b, n" l4 A# \<template>
4 f1 J. U6 [5 Y <div class="hello">
( ?$ |6 l, _4 `! V* C2 g( `4 a2 h6 W9 ?. C' r/ \ m
<h2>StateValue: {{this.$store.state.count}}</h2>& |8 v% N5 P9 J
<h2>GettesVaule: {{this.$store.getters.getStateCount}}</h2>1 H5 e5 {/ m: m
</div>2 {! {' n& R" H7 k* S
</template>
$ w! a( q7 \2 o; @/ E: ]6 u0 Z) C8 x
, P$ F9 o8 @% m l9 E
显示结果:
& i. j6 h1 \$ NStateValue: 1GettesVaule: 2
1 y* b" O) C( |4 Q( I6 A" `* O. W* T' u7 ?& }, M
(5)+ O; N1 {" ?9 ~7 I
Mutations: 数据我们在页面是获取到了,但是如果我们需要修改count值怎么办?如果需要修改store中的值唯一的方法就是提交mutation来修改,我们现在Hello World.vue文件中添加两个按钮,一个加1,一个减1;这里我们点击按钮调用addFun(执行加的方法)和reductionFun(执行减法的方法),然后在里面直接提交mutations中的方法修改值:
7 A: x# b: H4 h& g5 o! Y修改index.js文件,添加mutations,在mutations中定义两个函数,用来对count加1和减1,这里定义的两个方法就是上面commit提交的两个方法如下:6 N; e- h1 @2 f$ w) D
mutations:{
$ f8 K' J6 U* r/ S add(state){
' ]$ m% X% c- O. O: p& E" X* y state.count = state.count + 1;8 G& r( g5 S9 n: n* ~+ V3 {8 z
},2 y1 C% ?0 _/ L' ]3 q
reduce(state){
6 L0 e, v- A. j; L% j state.count = state.count - 1;
2 S/ ^8 q1 x" b! E/ L) M/ m0 J' i }3 I6 n3 M) u5 W) X
}# W2 |- ]$ Z# ^: k4 Z! l( L6 D
<template> f, K1 `$ z& j1 ~8 j6 }7 \
<div class="hello">" Y; O' v# ?3 d" i" d" ~, R
<h1>Actions:</h1>
8 J; A8 {, [5 \' X0 ]' d <el-button type="primary" @click="addFunc()">Add</el-button>' W* |' w M! S: U9 f" h9 `
<el-button type="Secondary" @click="reduceFunc()">Reduce</el-button>
@7 p6 E" H' `' X( l2 y <h2>StateValue: {{this.$store.state.count}}</h2>9 W2 Z8 C- }, F
<h2>GettesVaule: {{this.$store.getters.getStateCount}}</h2>$ w, u4 }% n3 `9 R
</div>
% A+ _: A8 b! L. i6 a- n</template>
% h4 i. I- N& l* {6 Q( ]5 R( I8 B2 {- ~5 E: @( b! J1 z
( Q1 F; W5 g1 \0 U" Y结果:% C/ i% c, w. F
! B$ v6 M: l" u2 {. _+ N6 s* m1 j, T. e. N4 R+ o
Actions:Add ReduceStateValue: 5GettesVaule: 67 g- r% S" `+ x4 ^! ~
) T, h3 z( C+ G" o p4 B4 l4 O9 ~+ N- e
; z2 V7 y& t9 R4 \3 q$ y% a+ m2 r A# |: O6 s$ ?' X0 L
- \+ E, S7 f4 m' ?( v
6 w/ R8 a6 I3 Z- P$ ?9 M9 c; @
0 F' R9 g' ~- q U8 W1 g, T0 F) w8 ?2 _' H3 c* ?
(6)Actions:
9 b t5 }6 n) w ]; b- n7 @我们看到,当点击三次后值从2变成了-1;页面上的值是改变了;我们达到了修改store中状态值的目的,但是,官方并不介意我们这样直接去修改store里面的值,而是让我们去提交一个actions,在actions中提交mutation再去修改状态值,接下来我们修改index.js文件,先定义actions提交mutation的函数:
( Z ~+ F( ^" i: h/ h1 }
# U% @5 [! b# G' Z. ^( W0 J' ]3 ^mutations:{
. o% }! v1 x! k- u" ^! F add(state){3 \. B# I1 h1 ~+ S
state.count = state.count + 1;
& v2 x/ B1 i- I& w },) ]% J2 V/ C; k% V+ c6 Z# i* @
reduce(state){" t9 N$ j, U& g0 K$ c
state.count = state.count - 1;0 q6 i! }" {5 y6 {' Z
}8 y0 A) z1 o, U1 R D* R( G( A
},
G2 q, m. g. V8 _ actions:{
* ]! n- N7 e# w6 b: [6 T addFunc(context){8 I; C3 E' o. R5 ]7 f7 Q3 _
context.commit("add");
4 d! p# m9 R! J5 S, L. e },5 l n X8 y% {: ^' h# k
reduceFunc(context){1 ~' d8 Z' L9 y) L
context.commit("reduce");
) m2 }+ |. x& t5 S/ u/ I }; K/ e, d1 U$ N+ s0 G' g2 }: [
}# r% j! X2 G8 t7 y; V5 a' M# H; j% W
4 X# @4 y% \- @! B1 B8 e
9 ?% P- @- N' a9 k' P$ @5 T4 F9 ~
2 b! N5 j& J3 Y( }" h, i methods:{
/ t! l$ K) R% S# _ addFunc(){
; Y9 M& f7 T* W5 }) d) \$ ~ //this.$store.commit("add");
" f# [6 z6 b3 G- j7 R this.$store.dispaTCh("addFunc");; o% U5 A: Z9 w, N) d+ q0 } I
},4 N/ u4 m. _% _: |! C& P
reduceFunc(){2 Y5 s" f$ d5 @, G( _* F# i$ U D1 |1 U
//this.$store.commit("reduce");
" K+ h4 Z, y# r- L { this.$store.dispatch("reduceFunc");* V* M5 M# [+ K: w; e& T
}
$ v; f2 `' _& b% P) I, [; l. z8 A }
- e9 t# p+ j, o0 i& e8 I' n" s- J
4 J- X" f. l" _3 i" O这里我们把commit提交mutations修改为使用dispatch来提交actions;我们点击页面,效果是一样的 K4 `( X" F( ^* i7 S' R
实现效果一样!!!!
9 o, A2 r c5 a; {7 h) K
9 ]9 k o+ R8 a4 @! m3 F s/ b3 y# G
5 O9 |2 e$ C- _% y1 U
- Q9 O4 @% y" K9 X* a: w! r! `. |
" ~8 p9 l; W! u; e& ^
% J9 W2 t8 P' t; l( X& ^4 u' g
8 t& f. h, o# b# J, `( ]. r ]( g+ J$ G. a
4 H1 `2 ]9 J5 \, M) g9 {: f4 I3 o
9 K; {* v# @8 A! K) m$ S" b7 k9 X
2 T3 J8 z. [) h" B% K; p0 H
8 G, @) W& Q% {1 B7 \
$ r( S& m/ P+ E" k5 h$ R
7 [+ D/ x9 f2 T$ p# k
, \/ U8 [" y( J! ~
# V3 R2 U4 _3 S | |
|