gpt4 book ai didi

javascript - 将脚本标签插入 Angular 4 HTML 组件

转载 作者:行者123 更新时间:2023-12-01 01:30:54 25 4
gpt4 key购买 nike

我正在尝试从该网站实现 HTML5 版本的 PhotoeditorSDK - https://docs.photoeditorsdk.com/guides/html5/v4/introduction/getting_started

我尝试使用 Angular Github repo 来实现但似乎 package.json 表明这只适用于 Angular 5 和 6,而我们的应用程序目前是 Angular 4.x 有点过时(我们目前无法升级到 5)

在简单的应用程序中使用 HTML5 方法相当容易,但在 Angular 4 中这似乎很棘手,因为由于 Angular 限制,我无法使用 <script>组件内的标签。

在索引中<head>我添加了以下内容:

<head>
<!-- React Dependencies for the SDK UI -->
<script src="js/vendor/react.production.min.js"></script>
<script src="js/vendor/react-dom.production.min.js"></script>
<!-- PhotoEditor SDK-->
<script src="js/PhotoEditorSDK.min.js"></script>
<!-- PhotoEditor SDK UI -->
<script src="js/PhotoEditorSDK.UI.DesktopUI.min.js"></script>
<link rel="stylesheet" href="css/PhotoEditorSDK.UI.DesktopUI.min.css"/>
</head>

模板本身有一个简单的 <div>如下:

<div id="editor" style="width: 100vw; height: 100vh;"></div>

脚本标签本身如下所示 - 基本上会附加一个图像,该图像将在编辑器中显示以进行编辑等。

<script>
window.onload = function () {
var image = new Image()
image.onload = function () {
var container = document.getElementById('editor')
var editor = new PhotoEditorSDK.UI.DesktopUI({
container: container,
// Please replace this with your license: https://www.photoeditorsdk.com/dashboard/subscriptions
license: '{"owner":"Imgly Inc.","version":"2.1", ...}',
editor: {
image: image
},
assets: {
// This should be the absolute path to your `assets` directory
baseUrl: '/assets'
}
})
}
image.src = './example.jpg'
}
</script>

我正在尝试找出使用 <script> 的最佳方法上面直接在 Angular 4 组件中 - 我知道这是最佳实践,但是最好的方法是什么?

最佳答案

您的组件有一个 id 为 editor 的元素。这仅在组件的 ngAfterViewInit Hook 中可用。一旦调用此函数,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'
}
}

关于javascript - 将脚本标签插入 Angular 4 HTML 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53283974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com