angular-cli build throws error属性类型' void'

时间:2017-05-10 18:45:22

标签: angular typescript angular-cli

我正在将角度2项目移动到角度4,我也将angular-cli更新为当前的1.0版本。应用程序中没有AOT。

我的应用中有一个组件在构建过程中抛出错误,但在IDE(Visual Studio 2017专业版)中没有。

构建期间抛出以下错误:

  错误在... / src / app / shift-list / shift-list.component.ts(163,88):   物业' userRank'在' void'。

类型中不存在      错误在... / src / app / shift-list / shift-list.component.ts(176,90):   物业' userPlatoon'在' void'。

类型中不存在

以下是我的组件(为简洁起见):

@Component({
  selector: 'ksm-shift-list',
  templateUrl: './shift-list.component.html',
  styleUrls: ['./shift-list.component.css'],
  providers: [
      DnnService
  ]
})
export class ShiftListComponent implements OnInit {

    ...
    userRank: string;
    userPlatoon: string;
    ...

    constructor(
        private _dnn: DnnService) { }

    // On Init  ********************************************************
    ngOnInit(): void {
        this.getDepartmentAndOptions();
    }

    // #################################################################
    //                        LIST OPTIONS
    // #################################################################

    getDepartmentAndOptions(): void {
        this._dnn.getOptionsAndProfile()
            .subscribe(
            data => {
                this.appOptions = data;

                this.ptnList = this.appOptions.Platoons;
                this.rankList = this.appOptions.Ranks;

                if (this.appOptions.User != null) {
                    this.userRank = this.appOptions.User.RankID;
                    this.userPlatoon = this.appOptions.User.PlatoonID;
                }
            }
            ),
            (err: any) => { // on error
                console.log(err);
            },
            () => { // on completion 

                this.sortRanks();
                this.sortPlatoons();
            }
    }


    // SORT RANK FUNCTION  **************** 
    sortRanks(): void {

        // sort rank list
        this.rankList.sort((b1: Rank, b2: Rank): number => {
            if (b1.Priority < b2.Priority) { return -1; };
            if (b1.Priority > b2.Priority) { return 1; };
            return 0;
        });

        // assign existing rank
        this.selectedRank = this.rankList.filter(function (r) { return r.Code === this.userRank })[0].RankID
    }

    // SORT PLATOONS FUNCTION   ***********
    sortPlatoons(): void {
        // sort results
        this.ptnList.sort((p1: Platoon, p2: Platoon): number => {
            if (p1.PlatoonID < p2.PlatoonID) { return -1; };
            if (p1.PlatoonID > p2.PlatoonID) { return 1; };
            return 0;
        });

        // assign existing platoon
        this.selectedPlatoon = this.ptnList.filter(function (p) { return p.Code === this.userPlatoon })[0].PlatoonID;
    }

}

我提到的错误会抛出以下两行:

this.selectedRank = this.rankList.filter(function (r) { return r.Code === this.userRank })[0].RankID

this.selectedPlatoon = this.ptnList.filter(function (p) { return p.Code === this.userPlatoon })[0].PlatoonID;

我没有更改任何代码,没有其他具有相同代码的组件会抛出此错误。

有人可以帮我弄清楚为什么在cli构建过程中会产生这个错误

提前致谢

1 个答案:

答案 0 :(得分:2)

this.ptnList.filter(function (p) { ..

应该是

this.ptnList.filter((p)=> { ..

相同
this.rankList.filter(function (r) { 

应该是

this.rankList.filter((r)=> {

如果您使用functionthis将引用该回调实例

建议阅读:How to access the correct `this` inside a callback?