diff --git a/public/docs/_examples/inheritance/ts/.gitignore b/public/docs/_examples/inheritance/ts/.gitignore new file mode 100644 index 0000000000..c20819f579 --- /dev/null +++ b/public/docs/_examples/inheritance/ts/.gitignore @@ -0,0 +1,7 @@ +**/*.ngfactory.ts +**/*.ngsummary.json +**/*.metadata.json +**/*.shim.ngstyle.ts +dist +!app/tsconfig.json +!rollup-config.js diff --git a/public/docs/_examples/inheritance/ts/README.md b/public/docs/_examples/inheritance/ts/README.md new file mode 100644 index 0000000000..d9172050ff --- /dev/null +++ b/public/docs/_examples/inheritance/ts/README.md @@ -0,0 +1,40 @@ +# Angular Inheritance Sample (draft) + +The AOT Cookbook sample was the starting point. +This sample runs in both JIT and AOT + +With AOT you have to re-build after each change. +With JIT you can develop on the fly as we usually do. + +## Inheritance experiments +Look at `sub.component.ts` to see the experiments in metadata inheritance. + + +## Building it + +The original source is in https://github.com/angular/angular.io/pull/3096 along with +tools and configs to build it in JIT and AOT. +You'll need knowledge of the Angular docs sample development practices to do it. + +Commands to run: + +``` +# compile with AOT, then rollup. Noisy. +npm run build:aot + +# start server and JIT/TS watching as usual +npm start +``` +The browser displays the JIT version (`index.html`) by default. +You can tell by looking at the browser tab (it says "JIT:..."), in the console, and in the page header. + +To see the AOT version, put `index-aot.html` in the address bar. +You can tell by looking at the browser tab (it says "AOT:..."), in the console, and in the page header. + +## Running it + +I like to have two console windows open: + +1. Running `npm start` (after once having run `npm run build:aot`) + +1. Where I periodically re-run either `npm run tsc` or `npm run build:aot` after a change that works in JIT diff --git a/public/docs/_examples/inheritance/ts/app/app.component.html b/public/docs/_examples/inheritance/ts/app/app.component.html new file mode 100644 index 0000000000..0601c5d61f --- /dev/null +++ b/public/docs/_examples/inheritance/ts/app/app.component.html @@ -0,0 +1,5 @@ + +
I am the base component. Koo-koo-ka-choo.
+{{services}}
+ `, + styleUrls: [ './base.component.css'] , + providers: [BASE_PROVIDERS] +}; + +@Component(BASE_METADATA) +export class BaseComponent { + @Input() speaker: string; + services: string; + + constructor(private a: ServiceA, private b: ServiceB) { + this.services = `ServiceA is ${a.name}; Service B is ${b.name}`; + } +} diff --git a/public/docs/_examples/inheritance/ts/app/main-aot.ts b/public/docs/_examples/inheritance/ts/app/main-aot.ts new file mode 100644 index 0000000000..8184fabe20 --- /dev/null +++ b/public/docs/_examples/inheritance/ts/app/main-aot.ts @@ -0,0 +1,7 @@ +// #docregion +import { platformBrowser } from '@angular/platform-browser'; +import { AppModuleNgFactory } from '../aot/app/app.module.ngfactory'; + +console.log('Running AOT version'); +document['aot'] = true; +platformBrowser().bootstrapModuleFactory(AppModuleNgFactory); diff --git a/public/docs/_examples/inheritance/ts/app/main.ts b/public/docs/_examples/inheritance/ts/app/main.ts new file mode 100644 index 0000000000..e3f504f2b8 --- /dev/null +++ b/public/docs/_examples/inheritance/ts/app/main.ts @@ -0,0 +1,7 @@ +// #docregion +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; +import { AppModule } from './app.module'; + +console.log('Running JIT version'); +document['aot'] = false; +platformBrowserDynamic().bootstrapModule(AppModule); diff --git a/public/docs/_examples/inheritance/ts/app/sub.component.ts b/public/docs/_examples/inheritance/ts/app/sub.component.ts new file mode 100644 index 0000000000..1a36a3c6d8 --- /dev/null +++ b/public/docs/_examples/inheritance/ts/app/sub.component.ts @@ -0,0 +1,69 @@ +import { Component, Injectable } from '@angular/core'; + +import { BaseComponent, BASE_METADATA, BASE_PROVIDERS, ServiceA } from './base.component'; + +///////// SubComponent service substitution //// +@Injectable() +export class ServiceASub { + name = 'A-sub'; +} + +///////// SubComponent Metadata Trials //// + +// The intended, fully specified SubComponent metadat. We know this works +const subMeta = { + moduleId: module.id, + selector: 'sub-comp', + template: ` +I am the SUB component. Hear me roar.
+{{services}}
+ `, + styleUrls: [ './base.component.css'] , + providers: [ + BASE_PROVIDERS, + {provide: ServiceA, useClass: ServiceASub} + ] +}; + +//////////////////// +// This works in JIT but not AOT +export function blendMetadata() { + return Object.assign({}, BASE_METADATA, subMeta); +} + +////////////////////////// +// Manual inheritance +const inheritMetadata = { + // inherit (silly) + moduleId: BASE_METADATA.moduleId, + + // Override + selector: 'sub-comp', + template: ` +I am the SUB component. Hear me roar.
+{{services}}
+ `, + + // Extend providers (actually overrides) + providers: [ + BASE_METADATA.providers, // inherit + {provide: ServiceA, useClass: ServiceASub} + ], + + // Inherit + styleUrls: BASE_METADATA.styleUrls // inherit +}; + +////////////// SubComponent //////////////////////// + +// This works in JIT and AOT +// @Component(subMeta) + +// This works in JIT but not AOT +// @Component(blendMetadata()) + +// This works in JIT and AOT +@Component(inheritMetadata) +export class SubComponent extends BaseComponent { } diff --git a/public/docs/_examples/inheritance/ts/example-config.json b/public/docs/_examples/inheritance/ts/example-config.json new file mode 100644 index 0000000000..1ef73390ce --- /dev/null +++ b/public/docs/_examples/inheritance/ts/example-config.json @@ -0,0 +1,4 @@ +{ + "build": "build:aot", + "run": "http-server:e2e" +} \ No newline at end of file diff --git a/public/docs/_examples/inheritance/ts/index-aot.html b/public/docs/_examples/inheritance/ts/index-aot.html new file mode 100644 index 0000000000..25fed2c421 --- /dev/null +++ b/public/docs/_examples/inheritance/ts/index-aot.html @@ -0,0 +1,24 @@ + + + + +