简体   繁体   English

使用 boolean 道具的 VueJS 计算属性未更新

[英]VueJS computed property using boolean prop not updating

I have a Vue (2.6.11 via Nuxt) component that gets a Boolean property from its parent and uses it to calculate additional computed properties.我有一个 Vue(2.6.11 via Nuxt)组件,它从其父级获取 Boolean 属性并使用它来计算其他计算属性。 After the initial rendering all values are shown as expected.初始渲染后,所有值都按预期显示。 However, when a parent flips the value of the passed-down property, only some values change in this component.但是,当父级翻转传递下来的属性的值时,该组件中只有一些值会发生变化。 Specifically DIVs bound directly to the property and original are both fine, but flipped and stringed never change again.具体来说,直接绑定到属性和original的 DIV 都很好,但flippedstringed永远不会再改变。

Assigning the original property to a local var before any evaluation within the computed property function makes no difference in the outcome.在计算属性 function 中进行任何评估之前,将原始属性分配给本地var对结果没有影响。

Changing computed properties to methods doesn't solve the issue either.将计算属性更改为方法也不能解决问题。 It is still just the first two that update properly.它仍然只是正确更新的前两个。

Note that the code below is stripped to a bare minimum to demonstrate the issue.请注意,为了演示该问题,下面的代码被剥离到最低限度。

<template>
  <div class="x">
    <div class="y">
      <div class="x">
        <div>{{ flag }}</div>
      </div>
      <div class="x">  
        <div>{{ original }}</div>
      </div>  
      <div class="x">  
        <div>{{ flipped }}</div>
      </div>  
      <div class="x">  
        <div>{{ stringed }}</div>
      </div>  
    </div> 
  </div>    
</template>

<script>
export default {
  name: "FlagBox",
  props: { 
    "flag": {
      type: Boolean
    }
  },
  computed: {
    original: function() {
      return this.flag;
    },
    flipped: function() {
      return !this.flag;
    },
    stringed: function() {
      return this.flag ? "yes" : "no";
    }
  }
}
</script>

What am I missing?我错过了什么? Thanks.谢谢。

I was not able to reproduce the issue but my best guess is that the radio button you are using is emitting a string value (eg, "false" instead of false) which is causing it to be true even when it's not.我无法重现该问题,但我最好的猜测是您正在使用的单选按钮正在发出一个字符串值(例如,“false”而不是 false),即使它不是,它也会导致它为 true。 Please check your props using the vue devtools to verify the data type being passed.请使用 vue devtools 检查您的 props 以验证传递的数据类型。 There is no other possible explanation for this, the code seems fine.对此没有其他可能的解释,代码似乎很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM