简体   繁体   English

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

[英]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"我正在使用 Angular 8 并尝试使用 Angular Elements 包“@angular/elements”中的 createCustomElement() 命令创建一个可重用的 Web 组件

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.它只包含一些包装 HTML 代码和一个带有 src 到徽标图像的 IMG 标记。 Here is my code.这是我的代码。

HTML 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 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.然后我将代码编译成 AppModule 内部的 Web 组件,就像这样。

   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.最后将导出的 JS 库拉入一个新的自定义 HTML 页面。

HTML 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.但我看到的只是 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. img 标签从不渲染。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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