Angular 角度2:找不到ngOnInit/Ngondestory

Angular 角度2:找不到ngOnInit/Ngondestory,angular,implements,ngoninit,ngondestroy,Angular,Implements,Ngoninit,Ngondestroy,注意:我找到了这篇文章,但下面的代码已经在导入OnInit/OnDestroy 我发现的所有示例,例如,都表明在类定义中添加implements OnInit/ondestory子句,并在订阅从路由获取参数时包括ngOnInit/ngondestory方法—代码正在执行此操作 但是,VS2017报告错误地为子句实现OnInit/OnDestroy,并报告错误找不到函数的ngOnInit/NgoEndstroy 如果我删除implements子句并注释ngOnInit/ngondestory函数,

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

我发现的所有示例,例如,都表明在类定义中添加implements OnInit/ondestory子句,并在订阅从路由获取参数时包括ngOnInit/ngondestory方法—代码正在执行此操作

但是,VS2017报告错误地为子句实现OnInit/OnDestroy,并报告错误找不到函数的ngOnInit/NgoEndstroy

如果我删除implements子句并注释ngOnInit/ngondestory函数,只在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));
    }
}
方法ngOnInit和Ngondestory应该在构造函数之外。像这样:

// imports ...

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

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

  ngOnInit() {
    // ...
  }

  ngOnDestroy() {
    // ...
  }
}

我可以从你的代码中看出ngOnInit和Ngondestory都在构造函数中,所以如果你把它带到外面,就可以了

代码示例:

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和Ngondestory时,应始终检查以下步骤:

检查是否已从“角度/核心”导入OnInit和OnDestroy 然后将它们添加到类中 添加这些函数

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

  ngOnDestroy(){}
}