0

注意:我发现文章在实现过程中在 typescript 中找不到 OnInit但下面的代码已经在导入OnInit/ OnDestroy

我发现的所有示例(例如,使用路由参数)都表明在类定义中添加implements OnInit/子句,并在订阅以从路由获取参数时包括/方法——代码正在这样做。OnDestroyngOnInitngOnDestroy

但是VS2017子句报错“错误实现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));
    }
}
4

2 回答 2

5

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

// imports ...

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

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

  ngOnInit() {
    // ...
  }

  ngOnDestroy() {
    // ...
  }
}
于 2018-07-03T15:49:32.493 回答
2

我可以从您的代码中看到 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' 导入 OnInit 和 OnDestroy
  2. 然后将它们添加到类
  3. 添加这些功能

    import { OnInit, OnDestroy } from '@angular/core';
    export class ComponentComponent implements OnInit, OnDestroy {
      ngOnInit(){}
    
      ngOnDestroy(){}
    }
    
于 2018-07-03T15:51:47.640 回答