[英]Can I extend an Angular 2 component without declaring the template every time
I have the following plunker . 我有以下矮人 。 It is fairly straight forward, however, now I would like to take advantage of TS Inheiratence so I create another constructor like this...
但是,这很简单,现在我想利用TS Inheiratence,因此我创建了另一个像这样的构造函数...
constructor(items: Array<NavItem>){
this.items = items;
console.log("New Route Called")
}
So I would like to do something like this... 所以我想做这样的事情...
@Component({
selector: "my-other-component"
})
export class MyOtherComponent extends App{
constructor(){
super([new NavItem("Other Test 1", "/test/1")]);
}
}
I tried something like this but couldn't get it working ... 我尝试过这样的事情,但无法正常工作 ...
But then I have to import the template multiple times which I would like to avoid. 但是然后我不得不多次导入模板,这是我想避免的。
The code is correct. 代码正确。 It just seems like the code order matters for plnkr... I've moved
App
to the beginning and it works. 似乎代码顺序对于plnkr很重要...我已经将
App
移到了开头,并且可以正常工作。 https://plnkr.co/edit/LukRyJ9OrzguyQlsgXxT?p=preview https://plnkr.co/edit/LukRyJ9OrzguyQlsgXxT?p=preview
You don't have to repeat your class properties definitions, so I've removed them too. 您不必重复类的属性定义,因此我也删除了它们。 Also it's good to know that you can have one constructor in
App
class with an optional parameter: 也很高兴知道您可以在
App
类中使用一个带有可选参数的构造函数:
constructor(items?: Array<NavItem>)
But then I have to import the template multiple times which I would like to avoid.
但是然后我不得不多次导入模板,这是我想避免的。
The only things which are not inherited are decorators (both class and method), so you basically need to copy them from the parent class. 唯一不继承的是装饰器(类和方法),因此您基本上需要从父类复制它们。 No way to get around... Hope it helps.
没办法解决...希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.