简体   繁体   English

我可以在不每次声明模板的情况下扩展Angular 2组件吗

[英]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.

相关问题 如何扩展 Angular 中的内置组件? - How can I extend a built in component in Angular? 我可以在ngFor循环中扩展角度分量并引用基本分量吗? - Can I extend an angular component and reference the base component in an ngFor loop? angular2扩展组件但保留模板 - angular2 extend component but keep template 使用 ng-template 扩展角度组件 - extend a angular component with ng-template 如何在Material Angular 8中扩展垫选择组件? - How can I extend mat-select component in Material Angular 8? 我可以在每次加载组件时和一次将模块一起加载而无需两次请求时解析服务吗? - Can i resolve services every time i load component and one time when i load module together without double request? 每次我使用路由导航到组件时,角度 http 获取 - angular http fetching every time i navigate to component using routing 我为什么不能使用 <template> 作为angular2组件模板? - Why can't I use <template> as an angular2 component template? 我可以为 Angular 中的每个加载的组件创建一个服务实例吗? - Can I create an instance of a service for every loaded component in Angular? Angular2 [2.4.10]导入子组件而不声明为AppModule - Angular2 [2.4.10] Import child component without declaring into AppModule
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM