计算属性返回props参数时报未定义异常

props: ['item'],
computed: {
        coinPrice () {
            return this.item.currentPrice.toFixed(2)
        }
}

第一次加载报的未定义异常,很奇怪?我查了百度说是数据请求的结果是异步执行的,所以第一次加载报未定义异常,那么该怎么解决呢?

阅读 2.8k
4 个回答

解决了,在data里面定义个属性来接收props里面的值,然后用data里面的属性来计算。perfect
props: ['item'],
computed: {

    coinPrice () {
        return this.i.currentPrice.toFixed(2)
    }

}
data(){
return{

i:item

}
}

给默认值呗

props: {
    item:{
        type: Object,
        default: () => ({currentPrice:0})
    }
}

你子组件咋写的 是不是传错值了

clipboard.png

computed: {
        coinPrice () {
            return typeof this.item.currentPrice === 'number' ? this.item.currentPrice.toFixed(2) :         
             this.item.currentPrice
        }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题