how to watch vuex state update

watch

https://vuex.vuejs.org/api/#watch

https://vuex.vuejs.org/guide/plugins.html

demo

this.$store.watch

this.$store.subscribe

https://dev.to/viniciuskneves/watch-for-vuex-state-changes-2mgj

...mapGetters

https://stackoverflow.com/questions/43270159/vue-js-2-how-to-watch-store-values-from-vuex


vuex watch demo

<template>
<el-select
v-model="ticketArea"
@change="selectChange"
class="live-area-select-box"
size="small"
placeholder="请选择票的选区">
<el-option
v-for="item in ticketAreas"
:key="item.value"
:label="item.label"
:value="item.value">
</el-option>
</el-select>
</template> <script> import axios from 'axios'; import {
createNamespacedHelpers,
} from 'vuex'; const {
mapState,
mapActions,
mapMutations,
mapGetters,
} = createNamespacedHelpers('seatSelect'); const log = console.log; export default {
name: 'AreaSelect',
props: {
ticketAreas: {
type: Array,
required: true,
default: () => [],
},
},
components: {},
data() {
return {
ticketArea: "",
};
},
watch: {
ticketAreas: function(newValue, oldValue) {
log(`\n\n\nticketAreas`, newValue);
if(newValue) {
this.updateTicketAreas(newValue || []);
}
},
},
computed: {
...mapState({
svgAreaSelected: state => state.svgAreaSelected,
storeSeatMap: state => state.seatMap,
storeSeatData: state => state.seatData,
isSVGEmpty: state => state.isSVGEmpty,
}),
// localComputed () { /* ... */ },
geoJSON () {
return JSON.stringify(this.$store.getters.geoJSON, null, 2)
},
...mapGetters([
// 'getSeatMap',
'getSVGEmpty',
]),
},
methods: {
...mapActions([
'saveGeoJSON',
'setTicketAreas',
]),
// ...mapActions({
// getSeatMap: 'getGeoJSON',// rename
// saveSeatMap: 'saveGeoJSON',// rename
// }),
updateTicketAreas(value){
this.ticketAreas = value || [];
},
selectChange(value){
log(`select 页面`, this.ticketAreas, value);
},
},
mounted() {
log(`ticketAreas xxxxx`, this.ticketAreas)
},
}
</script> <style lang="less">
.live-area-select-box{
box-sizing: border-box;
width: 100%;
min-width: 100px;
margin-bottom: 10px;
}
</style>

how to watch vuex state update的更多相关文章

  1. vue 关于deep watch / computed 监听不到 vuex state 对象变化的的问题

    简而言之,如果vuex state 中是一个对象 {},那么监听就会有问题.先给出解决方案: // 超简易拷贝(如果是深拷贝还多此一举把get/set拷贝进去了,所以用简易拷贝即可) let __VA ...

  2. vue自学入门-5(vuex state)

    vue自学入门-1(Windows下搭建vue环境) vue自学入门-2(vue创建项目) vue自学入门-3(vue第一个例子) vue自学入门-4(vue slot) vue自学入门-5(vuex ...

  3. [Nuxt] Update Vuex State with Mutations and MapMutations in Vue.js

    You commit changes to state in Vuex using defined mutations. You can easily access these state mutat ...

  4. weex里Vuex state使用storage持久化

    在weex里使用Vuex作为state管理工具,问题来了,如何使得state可以持久化呢?weex官方提供store模块,因此我们可以尝试使用该模块来持久化state. 先看下该模块介绍: stora ...

  5. Vuex state 状态浅解

    对于Vuex中的state里面的理解总是有些欠缺,机制似乎理解了.但是还有很多的不足,在这就先浅谈下自己的理解. vuex 机制中,定义了全局Store,在各个vue组件面的this.$store指向 ...

  6. vuex state使用

    访问vuex中的state值 方式1 <div>{{$store.state.count}}</div> 方式2 <template> <div id=&qu ...

  7. 万恶的浏览器缓存 Vuex state里面的成员改名后浏览器不会马上更新

    今天在用Vuex的时候,在state里面加了个名叫rootUrl的属性 但是怎么都取不到值,重新启动程序,ctrl+f5浏览器刷新都不行,纠结了大半上午,于是用console.log(store.ge ...

  8. Vue Vuex state mutations

    Vuex 解决不同组件的数据共享,与数据持久化 1.npm install vuex --save 2.新建store.js 并引入vue vuex ,Vue.use(Vuex) 3.state在vu ...

  9. vue v-model 与 vuex state数据绑定问题

    最近开发的项目 需要用input 的v-model 直接绑定到vuex的store数据 因为v-model 能与data的数据绑定 尝试了半天 代码如下 <template> <di ...

随机推荐

  1. try-catch 异常捕获学习

    #include <iostream> #include <string> #include <vector> #include <stdexcept> ...

  2. null调整为not null default xxx,不得不注意的坑

    最近碰到一个case,值得分享一下. 现象 一个DDL,将列的属性从null调整为not null default xxx, alter table slowtech.t1 modify name v ...

  3. cogs 2566 字符串最大值

    2566. [51nod 1129] 字符串最大值 ★★   输入文件:string_maxval.in   输出文件:string_maxval.out   简单对比时间限制:1 s   内存限制: ...

  4. 基于GTID恢复误篡改数据

    问题描述:创建测试库和测试表,先update数据,在delete数据,在update数据,通过gtid查找两次update的值. 参考文档:https://baijiahao.baidu.com/s? ...

  5. Java基础图解,JVM,线程,Spring,TCP,SpringMVC等开发体系图解

    Java基础图解,JVM,线程,Spring,TCP,SpringMVC等开发体系图解 1.Java虚拟机运行时数据区图 2. 堆的默认分配图 3.方法区结构图 4.对象的内存布局图 5.对象头的Ma ...

  6. SSM、SSH框架搭建,面试点总结

    文章目录 1.SSM如何搭建:三个框架的搭建: 2.SSM系统架构 3.SSM整合步骤 4.Spring,Spring MVC,MyBatis,Hibernate个人总结 5.面试资源 关于SSM.S ...

  7. 8. Linux重要的环境变量

    1.Linux 系统执行命令的4个步骤 第 1 步:判断用户是否以绝对路径或相对路径的方式输入命令(如/bin/ls),如果是的话则直接执行.  第 2 步:Linux 系统检查用户输入的命令是否为& ...

  8. jquery的ajax发送请求后前端不能实时更新

    在IE下用Ajax请求某一页面,通常会因为缓存的原因而返回上一次的结果,造成混乱(比如说多次请求却没有响应). 错误代码如下: $.get("fetch.php") .done(f ...

  9. SOLID:面向对象设计的五个基本原则

    在程序设计领域,SOLID 是由罗伯特·C·马丁在 21 世纪早期引入的记忆术首字母缩略字,指代了面向对象编程和面向对象设计的五个基本原则.当这些原则被一起应用时,它们使得一个程序员开发一个容易进行软 ...

  10. MyBatis逆向工程生成dao层增删改查方法解释使用(转载)

    int countByExample(BUserExample example); //根据条件查询数量 /** * 示例 * public int countByExample() { * BUse ...