VueX是适用于在Vue项目开发时使用的状态管理工具
安装
npm install vuex
在src路径下创建store文件夹,在App.js中引入store,在store文件夹中创建index.js
App.js
index.js
vuex核心内容
state 存放状态,在组建中this.
s
t
o
r
e
.
s
t
a
t
e
.
参
数
名
m
u
t
a
t
i
o
n
s
s
t
a
t
e
成
员
操
作
t
h
i
s
.
store.state.参数名 mutations state成员操作 this.
store.state.参数名mutationsstate成员操作this.store.state.commit(‘方法名’,参数列表)
getters 加工state成员给外界 this.
s
t
o
r
e
.
g
e
t
t
e
r
s
.
方
法
名
a
c
t
i
o
n
s
异
步
操
作
在
a
c
t
i
o
n
s
中
调
用
t
h
i
s
.
store.getters.方法名 actions 异步操作 在actions中 调用this.
store.getters.方法名actions异步操作在actions中调用this.store.getters.方法名
modules 模块化状态管理