[英]Rendering a Web Component in Angular?
在此演示中, HelloComponent
在渲染標記中包含一個 web 組件fs-image
。 這是組件:
import { Component, Input } from "@angular/core";
import { DomSanitizer, SafeHtml } from "@angular/platform-browser";
@Component({
selector: "hello",
template: `
<div [innerHTML]="safeHTML"></div>
`,
styles: [
`
h1 {
font-family: Lato;
}
`
]
})
export class HelloComponent {
constructor(private dm: DomSanitizer) {
this.safeHTML = this.dm.bypassSecurityTrustHtml(this.html);
}
@Input() name: string;
safeHTML: SafeHtml;
html: string = `
<h1> Web Component Test</h1>
<fs-image url="https://fireflysemantics.github.io/i/developer/instant-html-vscode.png"></fs-image>
`;
}
包含 web 組件的html
屬性通過DomSanitizer.bypassSecurityTrustHtml
傳遞,這樣 Angular 不會剝離自定義元素。
因此,當組件呈現時,它確實包含自定義元素,但它沒有呈現。 它應該像這樣渲染:
https://stackblitz.com/edit/typescript-fs-image-demo
web 組件通過 CDN 包含在 header 中。 CDN 聲明如下所示:
<head>
<script src="https://unpkg.com/@fireflysemantics/fs-image"></script>
</head>
關於它為什么不渲染的任何想法?
好的,我知道了。 在這種情況下, fs-image
使用100%
的寬度和高度,所以我不得不把它放在一個指定高度的容器中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.