How to Compile Angular Element Into Web Component w/ Webpack or Angular CLI?

后端 未结 4 1476
傲寒
傲寒 2021-02-13 04:06

I built a simple Web Component via Angular using Pascal Precht\'s tutorial, which you can see working HERE. It auto-magically compiles in the on Stackblitz in the link, but not

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-13 04:41

    import { NgModule} from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { HelloComponent } from './hello.component';
    import { AppComponent } from './app.component';
    
    @NgModule({
      imports: [BrowserModule],
      declarations: [AppComponent, HelloComponent],
      entryComponents: [HelloComponent],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    make sure you use

    npm install --save @angular/elements and add "@webcomponents/custom-elements" : "^1.0.8" in package.json. After that run npm install & along with that you need to un-comment below lines from polyfills.ts

    This adds a polyfill which is required for custom elements to work.

    import '@webcomponents/custom-elements/custom-elements.min'; import '@webcomponents/custom-elements/src/native-shim';

    stack Overflow

    Angular doesn't compile this above code, but angular elements fixes this issue by allowing to take our angular component and put it into totally encapsulated self bootstrapping HTML element which you can dump into your angular app in this following way for e.g and which will still work.

    In AppComponent.ts file

     import { Component, Injector } from '@angular/core'; 
     import { createCustomElement } from '@angular/elements'
     import { DomSanitizer } from '@angular/platform-browser';
    
     import { HelloComponent } from './hello.component';
    
     @Component({
      selector: 'app-root',
      template: '
    ', styleUrls: ['./app.component.css'] }) export class AppComponent { title = null; constructor(injector: Injector, domsanitizer: DomSanitizer){ const customElement = createCustomElement(HelloComponent, {injector: injector}); //this feature is not provided by angular it is provided by javascript //this allows us to register custom web component customElements.define('my-tag', customElement); //instead of 'hello-world' i've used 'my-tag' setTimeout(() => { //security mechanism is used to avoid cross site attacks this.title = domsanitizer.bypassSecurityTrustHtml('stack Overflow'); }, 1000); } }

    And inside your HelloComponent

     import { Component, OnInit, Input } from '@angular/core';
    
     @Component({
     selector: 'hello-world',
     template: `
    hello component -- {{ message }}
    `, styles: [` div { border: 1px solid black; background-color: red; padding: 1%; } `] }) export class HelloComponent implements OnInit { @Input() message : string; constructor() { } ngOnInit() { } }

    Now this is loaded as native web component.Still only usable in angular projects, but already usable for dyanamic content like this.

    I hope this will help you to run your code locally

提交回复
热议问题