子元件監聽父元件的傳值,如果兩次傳入同一個值,觸發不了監聽事件
阿新 • • 發佈:2020-08-24
做一個雙擊開啟彈窗的功能,因為別的元件中也用到彈窗中的內容,所以就將彈窗提出來做一個公共的元件。但是遇到的問題是,如果我兩次都雙擊同一個檔案,第二次就會觸發不了監聽事件。我的解決方式:
在父元件定義一個變數,初始化是0,雙擊的方法中變數自加,然後在子元件中監聽這個變數
父元件
<common-dialog :fileType="fileType" :fileId="fileId" :num = "num"></common-dialog> <script> data() { return { num: 0 } }, nextFolder(fileId, fileType) { this.num++; this.fileType = fileType; this.fileId = fileId; if (fileType == 0) { this.temp = []; this.oldParentId = this.queryForm.parentId; if (fileId == 0) { this.queryForm.fileType = 0; this.queryForm.parentId = undefined; } else { this.queryForm.fileType = undefined; this.queryForm.parentId = fileId; } this.getList(this.temp); } }</script>
子元件:
export default { props: ['fileType', 'fileId', 'num'], watch: { num: { handler(newVal, oldVal) { if (!newVal) { return; } switch (this.fileType) { case 1:this.picVisible = true; this.picSrc = showVideo(this.fileId); break; case 2: this.getPdfCode(fileId); break; case 3: this.videoVisible = true;this.videoSrc = showVideo(this.fileId); break; case 5: this.audioVisible = true; this.audioSrc = showVideo(this.fileId); break; } },
deep: true } }, }