vuex状态管理器
vuex核心概念
// vuex中一共有五个状态 State Getter Mutation Action Module
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
},
getters: {
},
// 里面定义方法,操作state方发
mutations: {
},
// 操作异步操作mutation
actions: {
},
modules: {
}
})
State
提供唯一的公共数据源,所有共享的数据统一放到store的state进行储存,相似与data
在vuex中state中定义数据,可以在任何组件中进行调用
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
//数据,相当于data
state: {
name:"张三",
age:12,
count:0
},
})
调用方式
- 方式一
在标签中直接 调用如:
<p>{{$store.state.name}}</p>
<p>{{$store.state.age}}</p>
- 方式二
// this.$store.state.全局数据名称如
<script>
export default{
data(){
return{
name:'',
age:this.$store.state.age
}
},
methods:{
giveName(){
thsi.name = this.$store.state.name
}
}
}
</script>
- 方式三
从vuex中按需导入mapstate函数如:
<script>
import { mapState } from "vuex";
export default{
data(){
return{
}
},
computed: {
...mapState([name, age])
}
}
</script>
Mutation
// 更改 Vuex 的 store 中的状态的唯一方法是提交 mutation。Vuex 中的 mutation 非常类似于事件:每个 mutation 都有一个字符串的事件类型 (type)和一个回调函数 (handler)。这个回调函数就是我们实际进行状态更改的地方,并且它会接受 state 作为第一个参数:
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
name: '张三',
age: 12,
count: 0
},
getters: {
},
// 里面定义方法,操作state方发
mutations: {
addCount(state, num) {
state.count =+ state.count+num
},
reduceCount(state) {
state.count--
}
},
// 操作异步操作mutation
actions: {
},
modules: {
}
})
- 组件的使用方法
// 方法一
<script>
export default{
data(){
return{
}
},
methods:{
onAddCount(){
this.$store.commit('addCount', 4)
},
onReduce () {
this.$store.commit('reduceCount')
}
}
}
</script>
// 方法二
<script>
export default{
data(){
return{
}
},
methods:{
...mapMutations(['addCount','reduceCount'])
onAddCount(){
this.addCount(4)
},
onReduce () {
this.reduceCount()
}
}
}
</script>
Action
Action和Mutation相似,Mutation 不能进行异步操作,若要进行异步操作,就得使用Action
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
name: '张三',
age: 12,
count: 0
},
getters: {
},
// 里面定义方法,操作state方发
mutations: {
addCount(state, num) {
state.count =+ state.count+num
},
reduceCount(state) {
state.count--
}
},
// 操作异步操作mutation
actions: {
asyncAdd (context) {
setTimeout(() => {
context.commit('reduceCount')
}, 1000);
}
},
modules: {
}
})
/*
* 调用方式
*/
// 方式一
this.$store.dispatch("asynAdd")
// 方式二
methods:{
...mapActions(['asyncAdd '])
onReduce () {
this.asyncAdd()
}
}
Module
mutation主要用于分割
在Vue中State使用是单一状态树结构,应该的所有的状态都放在state里面,如果项目比较复杂,那state是一个很大的对象,store对象也将对变得非常大,难于管理。
Vuex 允许我们将 store 分割成模块(module)。每个模块拥有自己的 state、mutation、action、getter、甚至是嵌套子模块——从上至下进行同样方式的分割:
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
},
getters: {
},
// 里面定义方法,操作state方发
mutations: {
},
// 操作异步操作mutation
actions: {
},
modules: {
userinfo: {
state: {
name: '李四'
},
getters: {},
modules: {}
},
header: {
state: {},
getters: {},
modules: {}
},
}
})
- 获取方式
this.$store.state.userinfo.name
getter
- getter是对于Store中的数据进行加工处理形成新的数据
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
counter: 2
},
getters: {
powerCounter(state) {
return state.counter * state.counter
},
},
// 里面定义方法,操作state方发
mutations: {
},
// 操作异步操作mutation
actions: {
},
modules: {}
})
- 调用方式
// 方法一
<p>{{$store.getters.powerCounter()}}</p>
// 方法二
<script>
import { mapGetters } from "vuex";
export default{
data(){
return{
}
},
computed: {
...mapGetters(['powerCounter'])
},
methods:{
onAddCount(){
this.powerCounter()
},
}
}
</script>
vuex状态管理器的更多相关文章
- VueX状态管理器 的应用
VueX状态管理器 cnpm i vuex axios -S 1 创建Vuex 仓库 import Vue from 'vue' import Vuex from 'vuex' vue.use(Vue ...
- vue项目--vuex状态管理器
本文取之官网和其他文章结合自己的理解用简单化的语言表达.用于自己的笔记记录,也希望能帮到其他小伙伴理解,学习更多的前端知识. Vuex 是什么? Vuex 是一个专为 Vue.js 应用程序开发的状态 ...
- vuex状态管理,state,getters,mutations,actons的简单使用(一)
之前的文章中讲过,组件之间的通讯我们可以用$children.$parent.$refs.props.data... 但问题来了,假如项目特别大,组件之间的通讯可能会变得十分复杂... 这个时候了我们 ...
- 组件之间的通讯:vuex状态管理,state,getters,mutations,actons的简单使用(一)
之前的文章中讲过,组件之间的通讯我们可以用$children.$parent.$refs.props.data... 但问题来了,假如项目特别大,组件之间的通讯可能会变得十分复杂... 这个时候了我们 ...
- vue状态管理器(用户登录简单应用)
技术点:通过vue状态管理器,对已经登录的用户显示不同的页面: 一 vue之状态管理器应用 主要用来存储cookie信息 与vue-cookies一起使用 安装:npm install vue-co ...
- vuex状态管理-数据改变不刷新
困惑: 在页面初始化的时候,我提交到vuex状态管理,然后在获取的时候获取不到,我找到了出错的地点,并进行了修改,然后可以获取到状态 但是不知道原因? 定义了如下的state const state ...
- 前端MVC Vue2学习总结(八)——Vue Router路由、Vuex状态管理、Element-UI
一.Vue Router路由 二.Vuex状态管理 三.Element-UI Element-UI是饿了么前端团队推出的一款基于Vue.js 2.0 的桌面端UI框架,手机端有对应框架是 Mint U ...
- vuex状态管理demo
vuex状态管理主要包含四个概念 mapState,mapMutations,mapGetters,mapActions. 编写vuex文件夹下面的store.js import Vue from ...
- 前端Vue框架-vuex状态管理详解
新人报道!多多关照-多提宝贵意见 谢谢- vuex理解 采用集中式存储管理模式.用来管理组件的状态,并以自定义规则去观测实时监听值得变化. 状态模式管理理解 属性 理解 state 驱动应用的数据源 ...
- 前端技术之:如何在vuex状态管理action异步调用结束后执行UI中的方法
一.问题的起源 最近在做vue.js项目时,遇到了vuex状态管理action与vue.js方法互相通信.互操作的问题.场景如下图所示: 二.第一种解决方法 例如,我们在页面初始化的时候,需要从服务端 ...
随机推荐
- 【博图scl语言】313-2dp
①如果 if(***) then *** := ***; end_if; ②循环 for n1:=1 to 50 by 1 do end_for; WHILE #n1 < 54 DO END_W ...
- P1002 [NOIP2002 普及组] 过河卒
P1002 [NOIP2002 普及组] 过河卒 题目见上. 一个经典的递推题 递推不会的看下面: https://www.cnblogs.com/haoningdeboke-2022/p/16247 ...
- gitignore文件中忽略项不起作用的解决方法
在使用git的时候会遇到这样的情况,我们生产的一些class或者target的目录,我不能提交,这个时候我们需要使用gitignore,但是有的时候虽然添加了,但是不起作用. 情况:开发过程中,我们自 ...
- Mac 远程 屏幕共享 screen sharing
屏幕共享可以用于在局域网中控制另一台 Mac,也能通过 iMessage 在广域网环境下创建彼此的连接,用来指导和解决问题非常方便. 通过 Apple ID 来创建连接 1,Command+空格键 ...
- linux 下 配置 nginx
服务器:centOS7 安装nginx之前操作: yum install -y pcre pcre-devel // pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http ...
- 成都信息工程大学第八届校赛 H J 题解
H. Bang Bang Keli Ba 题目大意 给定数组 \(a\) ,构造递增序列 \(b\) 和递减序列 \(c\) 且 \(a_i=b_i+c_i\) . 题解 下面证明解的存在性,存在性证 ...
- SQL 错误 [1105] [HY000]: errCode = 2, detailMessage = select list expression not produced by aggregation output (missing from GROUP BY clause?): ......
SQL 错误 [1105] [HY000]: errCode = 2, detailMessage = select list expression not produced by aggregati ...
- lombok安装不了的问题
- 9.java单链表初学代码复现及一些不值一提的小问题(2)
首先写完了update和delete函数,在之前的铺垫下.倒是不难,结构和之前的都相同,遍历找到节点后处理该节点.代码如下 public void update(teamNode node){ tea ...
- 算法学习—————数位dp
记忆化搜索版,比较有套路 就根据杠杆数这道题来回忆一下 题目大致意思:选定大数中的某个数作为支点,使左右两边的力矩和相等,求区间内能满足条件的数的个数 首先一个大前提:对于一个满足条件的数来说,他的支 ...