|
| 1 | +import { Component, Injectable } from '@angular/core'; |
| 2 | + |
| 3 | +import { BaseComponent, BASE_METADATA, BASE_PROVIDERS, ServiceA } from './base.component'; |
| 4 | + |
| 5 | +///////// SubComponent service substitution //// |
| 6 | +@Injectable() |
| 7 | +export class ServiceASub { |
| 8 | + name = 'A-sub'; |
| 9 | +} |
| 10 | + |
| 11 | +///////// SubComponent Metadata Trials //// |
| 12 | + |
| 13 | +// The intended, fully specified SubComponent metadat. We know this works |
| 14 | +const subMeta = { |
| 15 | + moduleId: module.id, |
| 16 | + selector: 'sub-comp', |
| 17 | + template: ` |
| 18 | + <h3>{{speaker}} sez:</h3> |
| 19 | + <p id="speak">I am the SUB component. Hear me roar.</p> |
| 20 | + <p>{{services}}</p> |
| 21 | + `, |
| 22 | + styleUrls: [ './base.component.css'] , |
| 23 | + providers: [ |
| 24 | + BASE_PROVIDERS, |
| 25 | + {provide: ServiceA, useClass: ServiceASub} |
| 26 | + ] |
| 27 | +}; |
| 28 | + |
| 29 | +//////////////////// |
| 30 | +// This works in JIT but not AOT |
| 31 | +export function blendMetadata() { |
| 32 | + return Object.assign({}, BASE_METADATA, subMeta); |
| 33 | +} |
| 34 | + |
| 35 | +////////////////////////// |
| 36 | +// Manual inheritance |
| 37 | +const inheritMetadata = { |
| 38 | + // inherit (silly) |
| 39 | + moduleId: BASE_METADATA.moduleId, |
| 40 | + |
| 41 | + // Override |
| 42 | + selector: 'sub-comp', |
| 43 | + template: ` |
| 44 | + <h3>{{speaker}} sez:</h3> |
| 45 | + <p id="speak">I am the SUB component. Hear me roar.</p> |
| 46 | + <p>{{services}}</p> |
| 47 | + `, |
| 48 | + |
| 49 | + // Extend providers (actually overrides) |
| 50 | + providers: [ |
| 51 | + BASE_METADATA.providers, // inherit |
| 52 | + {provide: ServiceA, useClass: ServiceASub} |
| 53 | + ], |
| 54 | + |
| 55 | + // Inherit |
| 56 | + styleUrls: BASE_METADATA.styleUrls // inherit |
| 57 | +}; |
| 58 | + |
| 59 | +////////////// SubComponent //////////////////////// |
| 60 | + |
| 61 | +// This works in JIT and AOT |
| 62 | +// @Component(subMeta) |
| 63 | + |
| 64 | +// This works in JIT but not AOT |
| 65 | +// @Component(blendMetadata()) |
| 66 | + |
| 67 | +// This works in JIT and AOT |
| 68 | +@Component(inheritMetadata) |
| 69 | +export class SubComponent extends BaseComponent { } |
0 commit comments