Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Web Components with Angular: Beyond the basics

Web Components with Angular: Beyond the basics

Manfred Steyer

March 22, 2019
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. @ManfredSteyer About me … • Manfred Steyer SOFTWAREarchitekt.at • Angular

    Trainings and Consultancy • Google Developer Expert (GDE) • Angular Trusted Collaborator Page ▪ 2 Manfred Steyer Public: Vienna, Munich, Frankfurt In-House: Everywhere in Europe http://www.softwarearchitekt.at/workshops
  2. @ManfredSteyer Contents • Motivation & Angular Elements 101 • Polyfills

    • Dynamic & Lazy elements • External elements • Dealing with dependencies • Excluding Zone.js
  3. @ManfredSteyer Angular Elements @NgModule({ imports: [BrowserModule], declarations: [DateComponent], bootstrap: [],

    entryComponents: [DateComponent] }) export class AppModule { constructor(private injector: Injector) { const DateElement = createCustomElement( DateComponent, { injector: this.injector }); customElements.define('date-picker', DateElement); } }
  4. @ManfredSteyer Selected Polyfills • webcomponents-loader.js • Loads Polyfills on demand

    • Custom Elements • Shadom DOM • Templates • native-shim.js • Needed for browsers that DO support polyfills … • … as long a we downlevel to ES5
  5. @ManfredSteyer Adding Custom Elements dynamically const tile = document.createElement('dashboard-tile‘); tile['prop']

    = 123; tile.setAttribute('class', 'col-lg-4 col-md-3 col-sm-2') tile.addEventListener([…])
  6. @ManfredSteyer Adding Custom Elements dynamically const tile = document.createElement('dashboard-tile‘); tile['prop']

    = 123; tile.setAttribute('class', 'col-lg-4 col-md-3 col-sm-2') tile.addEventListener([…]) content.appendChild(tile);
  7. @ManfredSteyer Register in angular.json @Injectable({ providedIn: 'root' }) export class

    LazyDashboardTileService { constructor( private loader: NgModuleFactoryLoader, private injector: Injector ) {} }
  8. @ManfredSteyer Register in angular.json @Injectable({ providedIn: 'root' }) export class

    LazyDashboardTileService { constructor( private loader: NgModuleFactoryLoader, private injector: Injector ) {} load(): Promise<void> { return this.loader.load(path).then(moduleFactory => { }); } }
  9. @ManfredSteyer Register in angular.json @Injectable({ providedIn: 'root' }) export class

    LazyDashboardTileService { constructor( private loader: NgModuleFactoryLoader, private injector: Injector ) {} load(): Promise<void> { return this.loader.load(path).then(moduleFactory => { moduleFactory.create(this.injector); […] }); } }
  10. @ManfredSteyer Register when bootstrapping @NgModule({ […] bootstrap: [] }) export

    class AppModule { constructor(private injector: Injector) { } ngDoBootstrap() { } }
  11. @ManfredSteyer Register when bootstrapping @NgModule({ […] bootstrap: [], }) export

    class AppModule { constructor(private injector: Injector) { } ngDoBootstrap() { const externalTileCE = createCustomElement( ExternalDashboardTileComponent, { injector: this.injector }); customElements.define('external-dashboard-tile', externalTileCE); } }
  12. @ManfredSteyer Load into consumer const script = document.createElement('script'); script.src =

    'assets/external-dashboard-tile.bundle.js'; document.body.appendChild(script);
  13. @ManfredSteyer Bundles Custom Element 1 Custom Element 2 Custom Element

    3 Libraries: Angular, RxJS, … Libraries: Angular, RxJS, …
  14. @ManfredSteyer Where Ivy can help ✓ Widgets based on @angular/core,

    @angular/common  Libs like @angular/forms or @angular/router
  15. @ManfredSteyer Sharing Libs Custom Element 1 Custom Element 2 Custom

    Element 3 Libraries: Angular, RxJS, … UMD Drawbacks: Complexity, no isolation
  16. @ManfredSteyer Contact and Downloads [mail] [email protected] [web] SOFTWAREarchitekt.at [twitter] ManfredSteyer

    d Slides & Examples Public and In-House http://www.softwarearchitekt.at/workshops