|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Vuex 的用法详解
7 x* d& I2 ]5 U' U7 Q' G) e, c3 h8 n+ w" B* V7 _* V3 v4 L6 W: K
本案例测试下Vuex的使用,创建一个新的web项目
" q- Y; p& z/ \+ | ~/ ]4 OVue init webpack web! ^ l$ C+ n" M9 K& D
安装vuex/ j. A, ~) M: c5 V0 A! z1 i
npm install vuex --save; d3 u- A$ a! n" w: z
启动, npm run dev,打开localhost:8080 即可
' R( N; S+ x$ t5 P6 Q p- }' \2 v7 p/ k3 @' V F
) O! z6 K- v# j0 b0 h
(1)引入vuex
" t, G% m8 E6 c+ {创建一个src/store文件夹,创建index.js , 引入并导出store
, _/ a7 d: L6 `- eimport Vue from 'vue'- ^% k" N E* _: u. Y
import Vuex from 'vuex'
# s3 D9 `; k VVue.use(Vuex);
8 g7 E* F, h. d; k) [ qconst store = new Vuex.Store({
3 O( |1 {; q, A3 \9 [. D})
: w7 k$ D- {' iexport default store;
0 O) {8 W" t4 l4 N/ B! f/ t$ m- ]& G5 x
(2)引入store到main.js
% M* Z, K+ Z R6 e4 \' jimport Vue from 'vue'$ ~" H, j% b- T; C% C9 g! l+ v' n
import App from './App'/ {# V8 e6 o# [3 W; M- G+ R
import store from './store'3 y$ L2 q# N# U# L$ W+ y
S" w, X% T+ d+ D7 t1 _8 p
Vue.config.productionTip = false0 F6 Q8 l% e5 J' d1 F; u1 f! ^) k- e
$ f/ x/ \9 b* |8 n, y0 O! X" j9 k+ n
/* eslint-disable no-new */% H( R5 d+ R% j0 [) @
new Vue({
7 }: M, B, w9 y' x el: '#app',* x. `! Y% n7 Q: D1 e7 w
store,* R$ K, C8 P# v7 ]' G3 f
components: { App },
: l, @7 A4 L f( i) f& l template: '<App/>'
9 d- A; c* [! p% k- I})/ S: D* z8 \! C: U9 v3 }# |, y
$ o- \, e: Q ^+ G9 z$ q2 h& k
3 v$ x) `3 [: \ r( K& Z/ w
: ~( v9 _3 {2 {0 i(3) 修改 helloword.vue ,先使用state获取值进行测试
! F7 H0 h1 a- n ^6 v, J. `* F, zState: vuex中的数据源,我们需要保存的数据就保存在这里,可以在页面通过 this.$store.state来获取我们定义的数据;- _7 p0 o# i- L0 n
const store = new Vuex.Store({3 ]2 U& f1 j, ?
state: {
) t& z: D- ?5 q& [7 e! d* j count:1
0 Q' p" y9 I1 f5 K5 D }5 W: d8 C& G6 [/ O1 z J
})
7 S# w1 Q' Q8 }2 x# U, ^5 z" M
+ s# E) G( @. m2 B& G& phelloword.vue----
% Y& I0 u6 l( n9 S7 Y<template>1 r7 o( H$ [5 `3 i
<div class="hello">6 h1 l. s1 V( n$ {, _! h
/ h3 m6 h8 V3 |
<h2>{{this.$store.state.count}}</h2>
8 A+ Q9 k* e) U2 Y1 c+ V* u9 q4 h8 Q- Z% s3 u
</div>' S# L- G* ~4 A3 `+ T
</template>
( @5 k, `8 O4 W& |' R0 q( B# ^# G+ n' M
. D4 ^6 W0 @, L8 p4 q显示结果为: 1
1 Y" {5 j5 v' a) V2 I4 {% W7 s; R. i4 Q" E8 H, Z. ]" W$ ^+ G
! {: O5 N: {# U) R8 o6 t$ G(4)Getter相当于vue中的computed计算属性,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算,这里我们可以通过定义vuex的Getter来获取,Getters 可以用于监听、state中的值的变化,返回计算后的结果,这里我们修改Hello World.vue文件如下:
. ^7 _0 H/ g+ V- y6 T0 Y- a$ H! T* M2 O9 [ J9 B0 e
const store = new Vuex.Store({+ a( w3 i% H# l. ^" L
state: {
$ I ?/ Y! {% Y count:1) A. P' `' V6 m4 w) R
},
4 G: \! s1 w) R) E) N getters:{$ d6 n5 t0 b8 q3 g
getStateCount:state=>{+ h) Q X3 E/ }( _5 `' A8 L
return state.count + 1;
/ K9 p6 v; d7 i$ L& x! c3 F, K/ a# O' w }
' O" n/ ]( N3 j( `; Y9 x }% [0 L9 k( I) p
})
" ]% {5 h) o' q' z) L, o
8 p q. s7 S/ J) y) c5 l# L<template>
& @: [ z Z4 g: Z! t' R <div class="hello">, m2 Y1 I L, R
1 w$ O) l! K6 E; p' S% p3 B <h2>StateValue: {{this.$store.state.count}}</h2>
0 U6 ?$ e$ { Y) Z: g2 L <h2>GettesVaule: {{this.$store.getters.getStateCount}}</h2>2 ]' T. |- g& c" Z
</div>" t3 \, E# S4 P+ h2 ?
</template>( G2 N' i: c# _7 b$ D$ v7 \
& Z9 M$ X3 t( F! w9 [
( x3 w; Z# A; H8 {/ g1 o显示结果:
1 S! f) d2 y. h6 r0 @StateValue: 1GettesVaule: 2& B, w5 i! ]- @: x B. F; h# d4 T- y8 a
& D, Q. U; Z' c* q: e
(5)3 C( @: `, a8 A) j, B$ I' h
Mutations: 数据我们在页面是获取到了,但是如果我们需要修改count值怎么办?如果需要修改store中的值唯一的方法就是提交mutation来修改,我们现在Hello World.vue文件中添加两个按钮,一个加1,一个减1;这里我们点击按钮调用addFun(执行加的方法)和reductionFun(执行减法的方法),然后在里面直接提交mutations中的方法修改值:
; f3 k: T Z8 W# n7 L9 a( A5 y修改index.js文件,添加mutations,在mutations中定义两个函数,用来对count加1和减1,这里定义的两个方法就是上面commit提交的两个方法如下:
9 K0 A* o! R& {mutations:{
1 b$ c5 H4 X- t( n% n$ B add(state){+ M( V) Q8 a; \8 Z7 T) X
state.count = state.count + 1;$ {1 T+ C# q* P# H: ~
},# Z8 n# f2 A) i/ |/ z! g) U
reduce(state){. v/ v% \2 D( r) X
state.count = state.count - 1;
' G. a" Q+ h& U* }$ s. F }
3 o9 w1 R9 L8 `; U/ }+ Y. c }
& Q$ n; o" S( ^2 s* m- r: }+ K<template>
/ ?2 b* c! R M' e% r <div class="hello">8 E" f! y$ s* W; y; P6 t9 S6 x3 R
<h1>Actions:</h1>
6 G; q5 R1 N" P }8 d- b <el-button type="primary" @click="addFunc()">Add</el-button>
, j# x9 x, p1 H <el-button type="Secondary" @click="reduceFunc()">Reduce</el-button>% r, v( s: ^( ?
<h2>StateValue: {{this.$store.state.count}}</h2>% h$ h. u& {' z6 f: C R
<h2>GettesVaule: {{this.$store.getters.getStateCount}}</h2>* O2 z+ B# N- _5 W* o; p
</div>
# @" f ^1 M$ V</template>
2 R0 c4 j7 W2 w2 r/ Z. j9 p, k f Z; d( c/ E6 g9 A
! f% L4 }- g* T
结果:
# \( J# W- P8 ~( k8 n1 f# u. B0 A# {8 }
; l3 r2 A9 t( g1 ?Actions:Add ReduceStateValue: 5GettesVaule: 6; N0 e) ?2 W C4 V* f
* r( }- l2 `. e! e& m" X+ a
8 S6 j. b7 d: X% I
8 |% q$ c6 l% s4 D1 a7 D4 E
3 G/ f: I7 P3 x( T# c! v& ]2 `3 w/ x- g% b: y; B
3 O4 w; b* a- j& S9 L3 z( Z
" G$ D# A8 b* n, L, d0 K; Y* r' K0 X' A# D7 D
(6)Actions:. ]- [% v/ f9 a
我们看到,当点击三次后值从2变成了-1;页面上的值是改变了;我们达到了修改store中状态值的目的,但是,官方并不介意我们这样直接去修改store里面的值,而是让我们去提交一个actions,在actions中提交mutation再去修改状态值,接下来我们修改index.js文件,先定义actions提交mutation的函数:
, m, f% o8 m6 q& H& _5 C % @' Q0 y! H5 S0 T8 V6 ^ ^, w
mutations:{! v' K' F" N7 K) k5 }) z
add(state){$ ]( S# }/ w7 U3 I% e
state.count = state.count + 1;
5 j$ D' `8 K: k. r: N9 S+ p },2 C! i: |& c$ h/ A: a& f3 g
reduce(state){) Y% L9 a3 }1 W
state.count = state.count - 1;
+ M* J, f' M' O S7 G& Q* m }8 @5 u3 \4 s5 z
}, {5 V/ l: h9 A
actions:{5 s3 x: p/ G; a; n0 ]: G, T
addFunc(context){
/ @+ S) D$ |* R8 A- C- w context.commit("add");
$ l; T% S% z! s. s+ P/ m$ c8 {6 O+ P4 g },
. g4 q" w( b7 M+ t: [ reduceFunc(context){
' H6 L2 u) ?6 w0 {( q context.commit("reduce");
/ {5 ]8 f$ w0 J2 o2 ?) f3 _' x }
+ J5 r" s5 M( w$ v9 v }, y- O; B; \) p2 v. r) V
0 \8 E4 ?5 {* H9 C; D
* u1 |$ V- u/ s, p
5 C; M) O$ b- n4 N B! a
methods:{
1 S# v& _* n" q addFunc(){. @5 [& J C! e2 ^$ B
//this.$store.commit("add");6 S* D* A. L4 {
this.$store.dispaTCh("addFunc");
/ J% d8 h6 j/ e) T( j. Z },5 Y, q, g5 G. E9 q4 \2 \ m
reduceFunc(){1 ?( \4 K% G3 Z3 K" {7 C
//this.$store.commit("reduce");7 Z) e8 z. o0 o5 y `% @
this.$store.dispatch("reduceFunc");
5 c7 {! l; S+ \% s }# Y5 Y8 s. _% W% ~
}- j* [0 ^% S+ c1 s
1 I5 L7 w( E/ D9 S; B7 j& k
这里我们把commit提交mutations修改为使用dispatch来提交actions;我们点击页面,效果是一样的
$ C! C% G; e- s/ T实现效果一样!!!!2 l8 K3 B6 s- E. Z
* X7 ~! u0 @" h/ R, N1 a6 d( r& w1 |; f$ l0 k6 S* q ^3 N8 I S* G. A
8 i* ^& y( P6 |% Q L
: v0 m3 u- v" D3 j8 x# U- W" r) W, m. z0 b$ w d n
' D# y1 n( F- ]; y# S
$ ^& f' g0 U& n- K2 r) E5 r* m/ h5 P
' }; S7 a7 g1 l" B/ c+ ]/ O3 F( {0 v# l
: ]$ _# U/ F- Z1 g) {9 T3 g4 T
- C0 ~0 f% ~% i4 w1 I
]2 j! y* Z6 I( _- N3 Z+ b
( D! U. Y3 m, G* W. G: G
, |- a: Z: L0 n8 U+ x) w& F! |- `8 s" T
" T& F. Q8 k* \ |
|