【问题标题】:Angular 2: Cannot find ngOnInit/ngOnDestroyAngular 2:找不到 ngOnInit/ngOnDestroy
【发布时间】:2018-12-12 00:02:32
【问题描述】:

注意:我找到了文章cannot find OnInit in typescript during implementation,但下面的代码已经在导入OnInit/OnDestroy

我发现的所有示例(例如,Using Route Parameters)都表明将implements OnInit/OnDestroy 子句添加到类定义中,并在订阅以获取参数时包括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));
    }
}

【问题讨论】:

    标签: angular implements ngoninit ngondestroy


    【解决方案1】:

    我可以从你的代码中看到 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(){}
      }
      

    【讨论】:

      【解决方案2】:

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

      // imports ...
      
      @Component({
        // ...
      })
      export class SubmissionComponent implements OnInit, OnDestroy {
        // ...
      
        constructor( /* ... */ ) {
          // ...
        }
      
        ngOnInit() {
          // ...
        }
      
        ngOnDestroy() {
          // ...
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-09
        • 2016-06-26
        • 2018-02-02
        • 2018-06-12
        • 2017-02-14
        • 2016-05-25
        • 2018-02-13
        • 2017-02-20
        相关资源
        最近更新 更多