VUE如何更新父元件繫結值
阿新 • • 發佈:2019-01-05
這個問題花了1天時間才搞明白,所以有必要記錄下。
這段時間使用vue,引入elementui中的步驟條元件,每一個步驟對應到一個元件頁面。在步驟裡點選下一步,因此需要更新父元件的active
的值:
<div class="newdeploy"> <keep-alive> <router-view :active.sync = "active"></router-view> </keep-alive> </div> <div class="step_box"> <el-steps :active="active" finish-status="success" simple style="margin-top: 20px"> <el-step title="選擇" ></el-step> <el-step title="編寫" ></el-step> <el-step title="確認" ></el-step> </el-steps> </div>
在子元件裡呼叫方法直接修改:
methods: {
next () {
if (this.active++ > 2) this.currentIndex = 0
},
console告警,不能直接修改prop的值:
Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop’s value. Prop being mutated: “active”
解決辦法如下,不直接修改active的值:
next () {
this.$emit('update:active', this.active + 1)
},