2
我刪除組件後刪除並重新創建任何組件後遇到問題。在被刪除並重新創建後,組件被刪除並創建子組件。刪除子組件會刪除該組件以及其後創建的所有子組件
這是發生這種情況的原因嗎?
小提琴代碼: :key
goes on the v-for
element:
Vue.component('column-component', {
props: ["columnData", "uniqueId"],
mounted: function() {
console.log('mounting column: ' + this.uniqueId)
},
beforeDestroy: function() {
console.log('removing: ' + this.uniqueId)
},
template: `
<div style="float: left; padding: 10px; margin-right: 10px; border: 1px solid black;">aaa</div>`
})
Vue.component('row-component', {
props: ["rowData", "uniqueId"],
data: function data() {
return {
columns: [],
columnCount: 0
}
},
mounted: function() {
console.log('mounting row: ' + this.uniqueId)
},
methods: {
addColumn() {
console.log
var column = new Object()
column.uniqueId = this.uniqueId +'.col.'+ this.columnCount
this.columns.push(column)
this.columnCount = this.columnCount + 1
}
},
beforeDestroy: function() {
console.log('removing: ' + this.uniqueId)
},
template: `
<div>
<div style="margin: 10px; padding: 20px; background: rgba(0,0,0, 0.5);">
row component: {{rowData.text}}
<div class="column" v-for="(column, index) in columns">
<column-component column-data="abc" :uniqueId="column.uniqueId"></column-component>
</div>
<div style="clear: both;"></div>
<div style="margin-top: 35px;">
<button @click="addColumn()">add column</button>
</div>
</div>
</div>`
})
new Vue({
el: '#app',
template: `
<div>
<div v-for="(row, index) in rows">
<row-component :uniqueId="row.uniqueId" :row-data="row" :key="row.uniqueId"></row-component>
<button @click="deleteThisRow(index)">remove row</button>
</div>
<button @click="addRow()">add</button>
</div>
`,
data: {
rows: [],
rowCount: 0
},
mounted: function() {
this.addRow()
this.addRow()
this.addRow()
},
methods: {
addRow() {
var row = new Object()
row.uniqueId = 'row-' + this.rowCount
row.text = 'row-'+(this.rows.length)
this.rows.push(row)
this.rowCount = this.rowCount + 1
},
deleteThisRow: function(index) {
this.rows.splice(index, 1)
console.log(this.rows)
}
}
})
這個怎麼樣: 「[Vue公司提醒]:避免使用非原始值爲鍵,使用字符串/數字值,而不是」我明白,如果我把對象,而不是字符串/數字。忽視它是否安全?此外,無論何時我對該行執行某些操作,添加一行或在行中添加一些內容等等,都會出現這種情況。不知道爲什麼它們會顯示出來? –
是的,當不使用密鑰,那麼它不會刪除所有組件,並重新創建它們,但它仍然無法正常工作,因爲它不會刪除正確的組件。任何方式的警告,或者我應該報告它作爲一個錯誤? –
btw我只注意到使用 row-component>也可以正常工作,所以它不會問題是如果傳入row或row.uniqueId,這種方法的問題是我想將v-包裝到div中,而不是直接在組件上使用它? –