Javascript 将计算属性作为属性传递(未定义)
我有一个Javascript 将计算属性作为属性传递(未定义),javascript,vue.js,vue-component,Javascript,Vue.js,Vue Component,我有一个salePrice()computed属性,我正试图将它传递给子组件。问题是组件表示salePrice未定义 我需要它是反应性的,但它似乎不起作用 修复和图像 这是salePricecomp。财产: salePrice(){ if(this.realestate.use_manual_price===true){ 返回此.realestate.manual\u价格 } 把这个还给我 }, 只要用户更改使用手册\u价格,就可以随时更改。在调试模式下,我看到它在应用程序中工作,但更改不
salePrice()
computed属性,我正试图将它传递给子组件。问题是组件表示salePrice
未定义
我需要它是反应性的,但它似乎不起作用
修复和图像
这是salePrice
comp。财产:
salePrice(){
if(this.realestate.use_manual_price===true){
返回此.realestate.manual\u价格
}
把这个还给我
},
只要用户更改使用手册\u价格
,就可以随时更改。在调试模式下,我看到它在应用程序中工作,但更改不会传播到子级
你知道怎么做吗?我还尝试在子组件中使用
this.$root.salePrice
。看起来应该可以工作,如果提供一个复制链接(如JSFIDLE)会更好
问题可能是您的computed属性实际上返回了一个未定义的值,因为this.realstate
if检查。您首先是如何定义或获取此值的
无论如何,你可以试试:
- 为
的道具定义提供默认值,例如salePrice
props:{ salesPrice: {type: Number, default: 0} }
- 或者也可以将另一个if检查添加到计算属性中,该属性返回一个默认值,以防该.realestate.price在该实例中也未定义
salePrice() {
if (this.realestate.use_manual_price === true) {
return this.realestate.manual_price
}
else if(this.realestate.price){
return this.realestate.price
}
return 'dummy price'
},