首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 4-模板继承

Angular 4-模板继承
EN

Stack Overflow用户
提问于 2017-04-12 17:15:09
回答 2查看 8.2K关注 0票数 12

我有以下子组件和它的继承父组件:

代码语言:javascript
复制
@Component({
   template: `<p>child</p>`
})
export class EditChildComponent extends EditViewComponent{
   constructor(){
     super();
   }
}



@Component({
   template: `<p>parent</p>`
})
export class EditViewComponent{
   constructor(){
   }
}

现在,有没有办法像处理嵌套组件中的ng- EditViewComponent那样将内容的模板放在ChildComponents模板中?如何获得以下结果:

代码语言:javascript
复制
<p>parent</p>
<p>child</p>
EN

回答 2

Stack Overflow用户

发布于 2020-01-13 14:17:07

目前,Angular不提供此功能。但是您可以使用"Narik“在angular中应用"UI继承”。

https://docs.narik.me/04.-ui-inheritance

票数 0
EN

Stack Overflow用户

发布于 2017-05-17 21:22:59

看看这篇文章:Angular 2, decorators and class inheritance

示例:plnkr.co

代码语言:javascript
复制
import {bootstrap} from 'angular2/platform/browser';
import {
  Component, 
  ElementRef,
  ComponentMetadata
} from 'angular2/core';
import { isPresent } from 'angular2/src/facade/lang';

export function CustomComponent(annotation: any) {
  return function (target: Function) {
    var parentTarget = Object.getPrototypeOf(target.prototype).constructor;
    var parentAnnotations = Reflect.getMetadata('annotations', parentTarget);

    var parentAnnotation = parentAnnotations[0];
    Object.keys(parentAnnotation).forEach(key => {
      if (isPresent(parentAnnotation[key])) {
        // verify is annotation typeof function
        if(typeof annotation[key] === 'function'){
          annotation[key] = annotation[key].call(this, parentAnnotation[key]);
        }else if(
        // force override in annotation base
        !isPresent(annotation[key])
        ){
          annotation[key] = parentAnnotation[key];
        }
      }
    });

    var metadata = new ComponentMetadata(annotation);

    Reflect.defineMetadata('annotations', [ metadata ], target);
  }
}

@Component({
  // create seletor base for test override property
  selector: 'master',
  template: `
    <div>Parent component</div>
  `
})
export class AbstractComponent {

}

@CustomComponent({
  // override property annotation
  //selector: 'sub',
  // ANSWER: it's not possible like that but you could have a function here. Something like:
  template: (template) => { return template + 'add html to temlate in child'}
  selector: (parentSelector) => { return parentSelector + 'sub'}
})
export class SubComponent extends AbstractComponent {
  constructor() {
    //console.log(super);
  }
}

@Component({
  selector: 'app',
  // CHECK: change view for teste components
  template: `
    <div>
      <div><sub>seletor sub not work</sub></div>
      <div><master>selector master not work</master></div>
      <div><mastersub>selector mastersub not work</mastersub></div>
    </div>
  `,
  directives [ SubComponent ]
})
export class App {

  constructor(private elementRef:ElementRef) {
  }

}

bootstrap(App).catch(err => console.error(err));
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43365274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档