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

标签 javascript angular angular-library angular-elements

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

所以组件本身非常简单。它仅包含一些包装器 HTML 代码和带有 Logo 图像 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 标签根本不会被渲染。

最佳答案

试试这个

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

关于javascript - 是否可以在 Angular Element 中包含一个 img src?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59413835/

相关文章:

javascript - Chrome 中指定的时区无效 : US/Alaska, US/Arizona、US/Mountain、US/Central 等错误但在 Mozilla 中工作

javascript - JqueryUI Draggable - 仅垂直自动调整父容器的大小

javascript - Angular 2 : How to prevent a form from submitting on keypress enter?

html - 使用<template ngFor>代替* ngFor时出现奇怪的间隙

Angular 库模块从模块导出组件、服务等

javascript - 当我移动 div 时,JavaScript 内的 Nettuts 不起作用

javascript - Angular 7 & Jest & Babel Integration over Jasmine/Karma - 找不到 snapshotSerializers 选项中的 AngularSnapshotSerializer.js

angular - 如何防止 Material 图标文本出现在 Material 选择中

css - 如何将全局样式添加到 Angular 6/7 库

Angular 库导出模型和库