父页面传值给子组件(解决了首次渲染子组件未更新的情况)

父页面(传totalData):

<govern-transaction-amount ref="governTransactionAmount" :totalData="totalData">
</govern-transaction-amount>

子组件接收: 

props: {
			totalData: Object,
		},
watch: {
	totalData: {
        immediate: true,
	    handler(newValue) {
		    this.newAmount = newValue.newAmount;
			this.collectedAmount = newValue.collectedAmount;
		}
	},
},

tips: 确保首次渲染更新,在监听中设置: immediate: true 

immediate: true :表示在watch中首次绑定的时候,是否执行handler,值为true则表示在watch中声明的时候,就立即执行handler方法,值为false,则和一般使用watch一样,在数据发生变化的时候才执行handler。所以当为true时 在created周期里就可以不用在写 已经在watch 中写过的方法了

猜你喜欢

转载自blog.csdn.net/weixin_44805839/article/details/130749468