【问题标题】:angular-cli build throws error Property does not exist on type 'void'angular-cli build 抛出错误“void”类型上不存在属性
【发布时间】:2017-10-09 13:46:30
【问题描述】:

我正在将 angular 2 项目移动到 angular 4,并且我还将 angular-cli 更新到当前的 1.0 版本。应用中没有 AOT。

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

在构建过程中抛出以下错误:

.../src/app/shift-list/shift-list.component.ts (163,88) 中的错误: 类型“void”上不存在属性“userRank”。

.../src/app/shift-list/shift-list.component.ts (176,90) 中的错误: 类型“void”上不存在属性“userPlatoon”。

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

@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构建过程中会产生这个错误

提前致谢

【问题讨论】:

    标签: angular typescript angular-cli


    【解决方案1】:
    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?

    【讨论】:

    • 哇,很好的捕捉,而且很快。谢谢,我必须等待 6 分钟才能接受 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    相关资源
    最近更新 更多