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

Web Components with Angular Elements: beyond the Basics @iJS Munich 2019

Web Components with Angular Elements: beyond the Basics @iJS Munich 2019

Manfred Steyer

October 26, 2019
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. @ManfredSteyer Contents • Basics • Dynamically adding elements • Lazy

    loading elements • External elements • Dealing with dependencies
  2. @ManfredSteyer About me … • Manfred Steyer SOFTWAREarchitekt.at • Angular

    Trainings and Consultancy • Google Developer Expert (GDE) • Angular Trusted Collaborator Page ▪ 10 Manfred Steyer Public: Selected Cities In-House: Everywhere http://www.softwarearchitekt.at/workshops
  3. @ManfredSteyer Angular Elements @NgModule({ imports: [BrowserModule], declarations: [DateComponent], entryComponents: [DateComponent]

    }) export class AppModule { constructor(private injector: Injector) { const DateElement = createCustomElement( DateComponent, { injector: this.injector }); customElements.define('date-picker', DateElement); } }
  4. @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([…])
  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([…]) content.appendChild(tile); Angular uses Domino to simulate document for SSR
  6. @ManfredSteyer Lazy Loading @Injectable({ providedIn: 'root' }) export class LazyDashboardTileService

    { constructor( private loader: NgModuleFactoryLoader, private injector: Injector ) {} }
  7. @ManfredSteyer Lazy Loading @Injectable({ providedIn: 'root' }) export class LazyDashboardTileService

    { constructor( private loader: NgModuleFactoryLoader, private injector: Injector ) {} load(): Promise<void> { return this.loader.load(path).then(moduleFactory => { }); } }
  8. @ManfredSteyer Lazy Loading @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); […] }); } }
  9. @ManfredSteyer Load into consumer const script = document.createElement('script'); script.src =

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

    3 Libraries: Angular, RxJS, … Libraries: Angular, RxJS, …
  11. @ManfredSteyer Angular Ivy Custom Element 1 Custom Element 2 Custom

    Element 3 Currently: No official Ivy/Elements Bridge Inofficial: See my blog (link comes later)
  12. @ManfredSteyer Roadmap (details can change) • Ivy behind a flag

    - try it out! • Currently no Ivy/Elements-Bridge Angular 8 (Spring 2019) • Ivy by default • No breaking changes Angular 9 (Fall 2019)
  13. @ManfredSteyer Where Ivy can help ✓ Widgets based on @angular/core,

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

    Element 3 Libraries: Angular, RxJS, … UMD Drawbacks: Complexity, no isolation