是否可以在Angular Element中包含img src?

我正在使用Angular 8,并尝试使用Angular Elements包“ @ angular / elements”中的createCustomElement()命令创建可重用的Web组件

因此,组件本身非常简单。它仅包含一些包装HTML代码和带有src到徽标图像的IMG标签。这是我的代码。

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;
    }
  }
}

因此,我将代码编译到AppModule内部的Web组件中,

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

最后将导出的JS库拉到新的自定义HTML页面中。

HTML

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

但是我所看到的只是HTML的一部分。

在浏览器中呈现为

<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>

img标签从不渲染。

wxslzx 回答:是否可以在Angular Element中包含img src?

尝试一下

<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>

或者这个

    <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>
本文链接:https://www.f2er.com/2890388.html

大家都在问