javascript - 如何在不触发 react 性的情况下计算 Vuex 状态差异

标签 javascript vue.js vuex

我有点击后改变 Vuex 状态的组件列表。 我想呈现两个 Vuex 状态值之间的差异:从点击处理程序调度的 Action 之前和之后。

可能是这样的:

<template>
<ul>
  <li v-for="item of items" @click="clickItem">{{difference}}</li>
</ul>
</template>
<script>
    export default {
        methods: {
              difference() {
                  let oldValue = this.$store.getters.getValue();
                  this.$store.dispatch(SOME_ACTION);
                  let newValue = this.$store.getters.getValue();
                  this.$store.dispatch(UNDO_SOME_CATION);
                  return newValue-oldValue;
              }
              clickItem() {
                  this.$store.dispatch(SOME_ACTION);
              }
        }
    }
</script>

但问题是这段代码会产生无限渲染循环,因为 Action 调度会改变 Vuex 存储和 Vuex react 性 进而触发应用程序组件重新呈现。

是否有可能禁用 Vuex react 性而不是分派(dispatch)操作,获取新值,而不是分派(dispatch)撤消操作并启用 react 性?如果是,它可以解决我的问题。

最佳答案

这是未经测试的,但我的想法是让 difference 从 IIFE(立即调用的函数表达式)返回它的功能,然后添加一个标志以指示它是否已被分派(dispatch)。

const difference = (function() {
  let dispatched;

  return function() {
    if (dispatched) {
      dispatched = false;
      return;
    }

    let oldValue = this.$store.getters.getValue();
    this.$store.dispatch(SOME_ACTION);
    dispatched = true;

    let newValue = this.$store.getters.getValue();
    this.$store.dispatch(UNDO_SOME_CATION);
    return newValue-oldValue;
  }
})()

关于javascript - 如何在不触发 react 性的情况下计算 Vuex 状态差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54925852/

相关文章:

javascript - 如何将 Vuex 列表中的项目传递给组件?

vue.js - 何时初始化 vue 和 vuex 状态

javascript - 为什么我无法填充我的数组对象?

javascript - undefined 不是 React Native 对象

javascript - 在不知道键名的情况下返回共享相同值的 JavaScript 对象键?

php - 如何使用 Heroku 部署 Laravel + Vue 项目

javascript - 避免在 React 中由对象文字 : how to do with variables in the object? 引起的重新渲染

vue.js - v-if 未在计算属性上触发

javascript - Vue : Show/Hide image based on its own src attribute

javascript - Vuex TypeScript 不更新 String,而是更新 Array