遇到的問題:
用el-dialog寫了個子組件
要實現在子組件中增刪數據 點擊確定后把值返回給父組件
父組件在每次點開子組件時都會把自己的值傳進去。
//父組件傳值
this.$refs.transfer.open(this.checkedColumn.concat(), this.columns.concat(), 'fbCampaign');
//子組件接受值
open(checkedColumn, columns, type) {
this.dialogVisible = true;
this.showColumns = checkedColumn;
this.otherColumns = columns;
this.type = type
}
邏輯並沒有錯誤。。。但會遇到下面問題。。
涉及刪除的操作,點保存沒有出現問題,點擊取消,父組件被刪除的數據就會不見。
但是並沒有傳值給父組件。
原因:
數組是引用類型,splice()會刪除所引用的地址里面的值。(吐血。。。)
以前在Java中遇到的問題,沒想到js也會有這種刪除問題。。
解決辦法
在父組件傳值的時候不傳原地址的參數,通過concat()函數復制一個新的值,再傳過去。
this.$refs.transfer.open(this.checkedColumn.concat(), this.columns.concat(), 'fbCampaign');
