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. 小步前进之WebService

    WebService Web Service 什么是Web Service? 为什么使用Web Service XML 什么是XML? 为什么使用XML? SOAP(Simple Object Acc ...

  2. 在项目中如何自定义的Eslint配置

    一.设置js风格的缩进为4个空格 在你的前端项目中找到.eslintrc.js文件,如图 module.exports = { root: true, parserOptions: { parser: ...

  3. Mac通过docker一键部署airflow

    目录 Airflow部署及使用 1.Dockerhub查看镜像地址 2.拉取docker镜像 3.在宿主机创建外挂文件夹 4.创建docker容器 5.重新创建docker容器 5.1.查看airfl ...

  4. Think in Java 第四 五 章

    Think in Java 第四章 控制执行流程 测试while public class whileTest { static boolean condition(){ boolean result ...

  5. sentinel流控规则校验之源码分析

    前言: 上节给大家把sentinel流控整个执行大致过了,但涉及到最核心的流控算法还没有讲,先提前说明一下 sentinel用的流控算法是令牌桶算法,参考了Guava的RateLimiter,有读过R ...

  6. 一文弄懂-BIO,NIO,AIO

    目录 一文弄懂-BIO,NIO,AIO 1. BIO: 同步阻塞IO模型 2. NIO: 同步非阻塞IO模型(多路复用) 3.Epoll函数详解 4.Redis线程模型 5. AIO: 异步非阻塞IO ...

  7. H - Oil Skimming (挖石油)

    题意大概是,海上漂浮着一些符号为#的石油,你要去搜集他们,但是你的勺子呢能且只能挖到两个单元的石油.问你最多能挖多少勺.注意 不能挖到纯净的海水,不然石油会被纯净的海水稀释的. 二分匹配,计算出里边有 ...

  8. P4254 [JSOI2008]Blue Mary开公司 (李超树)

    题意:插入一些一次函数线段 每次询问在x = x0处这些线段的最大值 题解:李超树模版题 维护优势线段 注意这题的输入是x=1时的b #include <iostream> #includ ...

  9. poj2443Set Operation (bitset)

    Description You are given N sets, the i-th set (represent by S(i)) have C(i) element (Here "set ...

  10. CodeForces - 721D 贪心+优先队列(整理一下优先队列排序情况)

    题意: 给你一个长度为n的数组,你可以对其中某个元素加上x或者减去x,这种操作你最多只能使用k次,让你输出操作后的数组,且保证这个数组所有元素的乘积尽可能小 题解: 在这之前我们要知道a*b>a ...