【发布时间】:2021-07-11 07:56:03
【问题描述】:
我试图从 DOM 中“窃取”由自己的组件生成的 SVG 代码。我是这样做的:
<my-own-component id="my-component-id" #myComponentId
[data]="data"></my-own-component>
onButtonClick() {
this.data = someData;
const svgCode = document.getElementById('my-component-id').innerHTML;
}
也试过了(也没有用):
@ViewChild('myComponentId') myComponentId;
...
onButtonClick() {
this.data = someData;
const svgCode = this.myComponentId.nativeElement.children[0].innerHTML;
}
问题是我得到内容之前 Angular已经应用了this.data = someData引起的变化,所以SVG的元素不包括在内。
我已经“解决”了它引入了 50 毫秒的超时。这可行,但不是正确的解决方案,是一个糟糕的补丁:
this.data = someData;
await new Promise(resolve => setTimeout(resolve.bind(null, null), 50));
const svgCode = document.getElementById('my-component-id').innerHTML;
我希望能够等待 Angular 完成渲染组件。有什么办法吗?
提前致谢。
【问题讨论】:
-
AfterViewInit 是否也为时过早?也许其中一个离子生命周期钩子可能对您有用:ionicframework.com/docs/angular/lifecycle
-
什么意思?
my-own-component.ngAfterViewInit还是my-page.ngAfterViewInit?我试过my-page的,没有成功。
标签: javascript angular typescript ionic-framework capacitor