參考文章:dataV組件庫——改變數據視圖不主動刷新
問題: 拿到后端數值就直接賦值了,但是視圖(頁面)沒有更新。
解決: 官方文檔介紹dataV里面的組件props均未設置deep監聽,刷新props時,要直接生成新的props對象(基礎數據類型除外),或完成賦值操作后使用ES6拓展運算符生成新的props對象(this.someProps = { …this.someProps }。
DataV官方文檔
代碼:
<template><div class="update-demo"><dv-percent-pond :config="config" style="width:200px;height:100px;" /></div>
</template><script>
export default {name: 'UpdateDemo',data () {return {config: {value: 66,lineDash: [10, 2]}}},methods: {// 更新數據的示例方法updateHandler () {const { config } = this/*** 只是這樣做是無效* config指向的內存地址沒有發生變化* 組件無法偵知數據變化*/this.config.value = 90this.config.lineDash = [10, 4]/*** 使用ES6拓展運算符生成新的props對象* 組件偵知數據變化 自動刷新狀態*/this.config = { ...this.config }}}
}
</script>