我正在使用 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/