角度2中的编译时错误:无法访问的代码

时间:2017-08-19 11:23:08

标签: angular angular-promise angular2-services es6-promise

在此行显示无法访问的代码

resolve(data)

造成这种情况的原因是什么?

我的代码:

  zipCodeBlurEventCall(zipcode) {
    if (!this.validationService.isValidZipCodeRequired(this.siteDetail.ZipCode)) {
        this.clearInvalidFlags();
        this.invalidZipCode = true;
        this.siteDetail.WeatherLocation = "";
        this.toastr.error("Please enter valid Zip Code");
    }
    else {            
        this.siteLoading = true;
        this.queryString = "?zipcode=" + zipcode;

         return new Promise((resolve, reject) => {
            this.siteService.getStationByZipCode(this.queryString).then(data => {
                this.element = data;

                if (this.element == null) {
                    this.siteLoading = false;
                    this.invalidZipCode = true;
                    this.toastr.error("Invalid Zip Code");
                    return true;

                } else {
                    this.siteDetail.StateName = this.states.find(item => item.StateId === this.siteDetail.StateId).StateName;
                    if (this.siteDetail.StateName.toLowerCase() == this.element.response.place.stateFull.toLowerCase()) {

                        this.siteDetail.WeatherLocation = this.element.response.place.name;
                        this.isWeatherFlag = true;
                        this.siteLoading = false;
                        this.invalidZipCode = false;
                        return false;
                    }

                    else {
                        this.siteLoading = false;
                        this.invalidZipCode = true;
                        this.toastr.error("Zip code does not match with the selected State");
                        return true;
                    }
                }

                resolve(data);                 
          });

    });            

    } 
}

1 个答案:

答案 0 :(得分:0)

我不确定你为什么要回归" true"或"假"这里的值,但如果您仍然希望代码工作,您可以执行以下操作,在调用resolve后从函数返回:

  zipCodeBlurEventCall(zipcode) {
    if (!this.validationService.isValidZipCodeRequired(this.siteDetail.ZipCode)) {
        this.clearInvalidFlags();
        this.invalidZipCode = true;
        this.siteDetail.WeatherLocation = "";
        this.toastr.error("Please enter valid Zip Code");
    }
    else {            
        this.siteLoading = true;
        this.queryString = "?zipcode=" + zipcode;

         return new Promise((resolve, reject) => {
            this.siteService.getStationByZipCode(this.queryString).then(data => {
                this.element = data;
                let returnVal;
                if (this.element == null) {
                    this.siteLoading = false;
                    this.invalidZipCode = true;
                    this.toastr.error("Invalid Zip Code");
                    returnVal = true;

                } else {
                    this.siteDetail.StateName = this.states.find(item => item.StateId === this.siteDetail.StateId).StateName;
                    if (this.siteDetail.StateName.toLowerCase() == this.element.response.place.stateFull.toLowerCase()) {

                        this.siteDetail.WeatherLocation = this.element.response.place.name;
                        this.isWeatherFlag = true;
                        this.siteLoading = false;
                        this.invalidZipCode = false;
                        returnVal = false;
                    }

                    else {
                        this.siteLoading = false;
                        this.invalidZipCode = true;
                        this.toastr.error("Zip code does not match with the selected State");
                        returnVal = true;
                    }
                }

                resolve(data);
                return returnVal;               
          });

    });            

    } 
}