简体   繁体   English

将 prop 值从父级传递给子级时的 v-model 值更新问题

[英]v-model value update issue when passed prop value from parent to child

I am passing an object 'item' from parent to 'child' component.我正在将对象“项目”从父组件传递给“子”组件。 It works just fine.它工作得很好。 Please refer this .请参考这个

As you change the values from dropdown, it updates the UI.当您从下拉列表中更改值时,它会更新 UI。 My issue is that the same exact code does not work in my application (running locally on my machine).我的问题是相同的代码在我的应用程序中不起作用(在我的机器上本地运行)。 I even tried adding {{item.type}} in html, but it does not change (sticks to original value).我什至尝试在 html 中添加{{item.type}} ,但它没有改变(坚持原始值)。 One thing I noticed that, if I put @change='onChange' and printed the value in onChange method and it prints updated value.我注意到的一件事是,如果我输入@change='onChange'并在onChange方法中打印值,它会打印更新的值。

Really unable to find solution to fix this.真的无法找到解决此问题的解决方案。 Any help would be great.任何帮助都会很棒。 Thanks.谢谢。

The issue is you are adding the type property to your model after the item was already bound to data, and Vue cannot detect changes to properties added that way.问题是您在项目已经绑定到数据之后将type属性添加到模型中,而Vue 无法检测到以这种方式添加的属性的更改

The fix is to make sure there is a type property on item ,解决方法是确保item上有一个type属性,

item: {
  "direct_sale_price": "",
  "is_auction": true,
  "is_tender": false,
  "type": null
}

or to properly add it using $set .或使用$set正确添加它。

created: function () {
  if (this.item.is_auction) {
    this.$set(this.item, 'type', 'auction')
  } else if (this.item.direct_sale_price) {
    this.$set(this.item, 'type', 'direct-sale')
  } else if (this.item.is_tender) {
    this.$set(this.item, 'type', 'tender')
  } else {
    this.$set(this.item, 'type', 'plain')
  }
}

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

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