簡體   English   中英

未捕獲的TypeError:無法讀取null的屬性“ hide”

[英]Uncaught TypeError: Cannot read property 'hide' of null

這是我的代碼

var ReportFolderListUtil = {
    confirmBox2:null,
    showConfirmation: function(reportFolderId){
        console.log(reportFolderId);

        this.confirmBox2 = Lobibox.confirm({
            delay:false,
            title : 'Confirmation',
            msg : "Please take note that reports created under this folder will also be deleted. Are you sure you want to delete this folder?",
            buttons: {
                yes: {
                    'class': 'btn btn-success',
                    closeOnClick: false
                },
                no: {
                    'class': 'btn btn-default',
                    closeOnClick: true
                }
            },
            callback: function ($this, type, ev) {
                if (type === 'yes')
                    window.location.href = "/report/remove-folder/"+reportFolderId+".html";
                    ReportFolderListUtil.hideConfirmation();
                    JobUtil.showLoading("deleteReport","deleting folder.");
                }
            }
        })
    },
    hideConfirmation: function(){
        this.confirmBox2.hide();
    }

我在控制台上不斷收到此錯誤:

在此處輸入圖片說明

我能理解你的代碼,問題是,你在呼喚ReportFolderListUtil.hideConfirmation方法之前ReportFolderListUtil.showConfirmation方法,所以ReportFolderListUtil.confirmBox2不是重新初始化this.confirmBox2 = Lobibox.confirmshowConfirmation方法體,它是nullReportFolderListUtil變量聲明。

根據您的需要,您可能會

a)以hideConfirmation的方式保護hideConfirmation

hideConfirmation: function() {
    this.confirmBox2 && this.confirmBox2.hide();
}

b)或定義隱藏方法存根

var ReportFolderListUtil = {
  confirmBox2: { 
    hide: function () { }
  },

c)或查看方法調用的順序,以免在顯示前調用hide ...

在此上下文中執行“ showConfirmation”之前,confirmBox2將沒有值。 您在回調中的“ if”上也缺少大括號。

在執行功能時牢記對象的狀態很重要。 為了防止發生致命錯誤,我建議進行更多的null檢查if( confirmbox2 ) { //execute request }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM