Angular 2: 找不到 ngOnInit/ngOnDestroy

Angular 2: Cannot find ngOnInit/ngOnDestroy

注意:我找到了文章 但下面的代码已经导入 OnInit/OnDestroy.

我找到的所有示例(例如 Using Route Parameters)都表明将 implements OnInit/OnDestroy 子句添加到 class 定义并包括 ngOnInit/ngOnDestroy 订阅从路由获取参数时的方法——代码正在做的事情。

但是VS2017子句报错"incorrectly implements OnInit/OnDestroy",函数报错"Cannot find ngOnInit/ngOnDestroy"

如果我删除 implements 子句并注释 ngOnInit/ngOnDestroy 函数(仅在 ngOnInit 的正文中保留代码),则代码有效;成功从路由参数中获取参数

import { Component, Inject, OnInit, OnDestroy } from '@angular/core';
import { Http } from '@angular/http';
import { ActivatedRoute } from '@angular/router';

@Component({
    selector: 'submission',
    templateUrl: './submission.component.html'
})
export class SubmissionComponent implements OnInit, OnDestroy {
    private baseUrl: string;
    private http: Http;
    private route: ActivatedRoute;
    private sub: any;

    public caseId: string;
    public submission: Submission;

    constructor(http: Http, route: ActivatedRoute, @Inject('BASE_URL') baseUrl: string) {
        this.http = http;
        this.route = route;
        this.baseUrl = baseUrl;

        ngOnInit() {
            this.sub = this.route.params.subscribe(params => {
                this.caseId = params['caseId'];

                // In a real app: dispatch action to load the details here.
            });
        }

        ngOnDestroy() {
            this.sub.unsubscribe();
        }

        this.get(this.caseId);
    }

    public get(caseId: string) {
        var url = this.baseUrl + 'api/Submission/GetSubmission?caseId=' + caseId;
        this.http.get(url).subscribe(result => {
            this.submission = result.json() as Submission;
        }, error => console.error(error));
    }

    public put() {
        var url = this.baseUrl + 'api/Submission/PutSubmission';
        this.http.put(url, this.submission).subscribe(result => {
            this.submission = result.json() as Submission;
        }, error => console.error(error));
    }
}

ngOnInitngOnDestroy 方法应该在构造函数之外。像这样:

// imports ...

@Component({
  // ...
})
export class SubmissionComponent implements OnInit, OnDestroy {
  // ...

  constructor( /* ... */ ) {
    // ...
  }

  ngOnInit() {
    // ...
  }

  ngOnDestroy() {
    // ...
  }
}

我从你的代码中可以看出 ngOnInit 和 ngOnDestroy 在构造函数内部,所以如果你把它带到外面应该没问题。

代码示例:

constructor(http: Http, route: ActivatedRoute, @Inject('BASE_URL') baseUrl: string) {
    this.http = http;
    this.route = route;
    this.baseUrl = baseUrl;
}

ngOnInit() {
    this.sub = this.route.params.subscribe(params => {
      this.caseId = params['caseId'];

      this.get(this.caseId);

      // In a real app: dispatch action to load the details here.
    });
}

ngOnDestroy() {
  this.sub.unsubscribe();
}

通常,在添加 ngOnInit 和 ngOnDestroy 时,您应该始终检查以下步骤:

  1. 检查您是否从 'angular/core'
  2. 导入了 OnInit 和 OnDestroy
  3. 然后将它们添加到 class
  4. 添加这些函数

    import { OnInit, OnDestroy } from '@angular/core';
    export class ComponentComponent implements OnInit, OnDestroy {
      ngOnInit(){}
    
      ngOnDestroy(){}
    }