【发布时间】:2018-12-19 02:58:09
【问题描述】:
我使用 import {Ng2StateDeclaration} from '@uirouter/angular'; 我现在在 Angular 5 中遇到了一个问题 我有没有模板的 Paper 类:
export class Paper implements OnInit {
ngOnInit(): void {
// async function here
this.loadData();
}
}
现在我有了另一个名为 PaperEdit 的类:
export class PaperEdit extends Paper{
constructor(){super();}
}
在 PaperEdit 类中,我有很多带有长 html 的部分: paper-edit.html
<paper-edit-form-1></paper-edit-form-1>
<paper-edit-form-2></paper-edit-form-2>
<paper-edit-form-3></paper-edit-form-3>
PaperEditForm1,2,3是一个只有html文件的类,没有逻辑,我想继承Paper类的所有变量和函数,比如PaperEdit
export class PaperEditForm1 extends Paper {
constructor(){super();}
// no function here`
}
结果是:Paper 类的 ngOnInit 运行了 4 次,这意味着 this.loadData() 也运行了 4 次。 因此,任何防止此问题的方法,我希望该生命周期只运行 1 次!谢谢。
【问题讨论】:
-
你不能让 4 个组件的生命周期只运行一次。所以这个问题没有意义。
-
@cgTag:是的。我正在使用
import {Ng2StateDeclaration} from '@uirouter/angular';。这意味着在 url:edit/view 页面中,我将首先加载 Paper 组件。该组件的样式为空。如果在 url: '/edit+ id' 中,我将加载 PaperEdit 组件 -
我看不出您的问题与路由有何关系?
标签: angular class inheritance duplicates