vue回調函數無法更改model的值


data:{
isUpload:true,
} 

點擊上傳函數:

 getFile(event) {
//選擇圖片
               let eventId = event.target.id;
                 let type=  testImgType(eventId);
                if(!type){
                    return;
                }
                let fileName = event.target.files[0].name;
                let max = testMaxSize(event.target.files[0],1024*3*1024);
                if(!max){
                    return;
                }
                let width = testImgWidthHeight(eventId,1920,1080,function (res) {
                    if (!res) {
                        this.isUpload=false;
                        console.log( this.isUpload)
                        vue.$message({
                            message: '圖片尺寸不正確!',
                            type: 'warning'
                        });
                    }else {
                        this.isUpload=true;
                        console.log( this.isUpload)
                    }
                });
                if(eventId=='addWorkFile'){
                    this.file = event.target.files[0];
                    this.workFileName=fileName;
                }else{
                    this.file2 = event.target.files[0];
                    this.personFileName=fileName;
                }
            },
//點擊上傳
            submitForm(event) {
                console.log(this.isUpload)
                if(!this.isUpload){
                    console.log(this.isUpload)
                    return;
                }

結果isUpload的值沒有變化,

原因:

在請求執行成功后執行回調函數中的內容,回調函數處於其它函數的內部this不會與任何對象綁定

解決辦法:一)將指向vue對象的this賦值給外部方法定義的屬性,然后在內部方法中使用該屬性

二)使用箭頭函數

 

 

看更改后的結果:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM