简体   繁体   中英

Is it possible to include an img src inside of Angular Element?

I'm working with Angular 8 and trying to create a reusable Web Component using the createCustomElement() command in the Angular Elements package "@angular/elements"

So the component itself is very simple. It just contains a some wrapper HTML code and anIMG tag with a src to a logo image. Here is my code.

HTML

<a [routerLink]="homeURL" class="logo-branding no-link flex-grow-2">
  <img class="logo align-middle" [src]="headerImgPath">
  <span class="branding text-nowrap">{{ this.headerBranding }}</span>
</a>

TS

import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'header-logo',
  templateUrl: './header-logo.component.html',
  styleUrls: ['./header-logo.component.scss'],
})
export class HeaderLogoComponent implements OnInit {
  @Input() logo: string;
  @Input() branding: string;
  @Input() url: string;

  public headerImgPath = 'elements-assets/images/my-logo.svg';
  public headerBranding = 'Platform Name';
  public homeURL: string;

  constructor() {}

  ngOnInit() {
    if (this.branding) {
      this.headerBranding = this.branding;
    }

    if (this.logo) {
      this.headerImgPath = this.logo;
    }

    if (this.url) {
      this.homeURL = this.url;
    }
  }
}

So then I compile the code into a web component inside of the AppModule, like so.

   const el = createCustomElement(LogoComponent, { injector: this.injector });
   customElements.define('page-logo', el);

And finally pull the exported JS library into a new custom HTML page.

HTML

<div class="container" style="position: relative; height:200px; top: 100px;">
  <page-logo></page-logo>
</div>

But what I see is only part of the HTML.

Render in the browser as

<div class="container" style="position: relative; height:200px; top: 100px; >

    <my-logo _ngcontent-trw-c0="">
       <a class="logo-branding no-link flex-grow-2"></a>
    </page-logo>

</div>

The img tag is never rendered at all.

Try this

<a [routerLink]="homeURL" class="logo-branding no-link flex-grow-2">
  <img class="logo align-middle" [src]="elements-assets/images/my-logo.svg">
  <span class="branding text-nowrap">{{ this.headerBranding }}</span>
</a>

Or this

    <a [routerLink]="homeURL" class="logo-branding no-link flex-grow-2">
      <img class="logo align-middle" [src]="this.logo">
      <span class="branding text-nowrap">{{ this.headerBranding }}</span>
    </a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM