您的组件有一个id为的元素
editor
ngAfterViewInit
window.onload
很久以前就有了。当
onload
被称为,元素根本不存在,所以把它放在那里也是个坏主意。
最好是从
ngAfterViewInit
@ViewChild
注释:
declare const PhotoEditorSDK: any;
@Component({
template: `<div style="width: 100vw; height: 100vh;" #editor></div>`
})
export class EditorComponent implements AfterViewInit {
@ViewChild('editor') editor: ElementRef<HTMLElement>;
ngAfterViewInit(): void {
const image = new Image();
image.addEventListener('load', () => {
const editor = new PhotoEditorSDK.UI.DesktopUI({
container: this.editor.nativeElement,
license: '{"owner":"Imgly Inc.","version":"2.1", ...}',
editor: {
image
},
assets: {
baseUrl: '/assets'
}
});
});
image.src = './example.jpg'
}
}