【发布时间】:2019-09-17 10:29:48
【问题描述】:
在我的 react 应用程序中,我从服务器获取自定义 javascript 文件并将其作为 script 标记附加到 document 的正文中。
这个新添加的自定义文件包含一个名为manipulator 的方法。现在在其中一个组件中,我想调用该函数。据我所知,该函数应该存在于window 全局对象中。
if(documnet.getElementById('customJsId')){ // check if the script tag exists in document
window.manipulator(); // which I get Property 'iframeManipulator' does not exist on type 'Window'.ts(2339)
}
但是在这里我得到编译器错误
类型“Window”.ts(2339) 上不存在属性“manipulator”
这完全合乎逻辑,但我没有找到为window 创建扩展接口的方法或任何其他方法来告诉编译器window 中有一个名为manipulator 的可选函数。
感谢任何帮助。
----------Just in case--how the script is added to document--------------
loadProjectCustomJS() {
runInAction(async () => {
thisfetchProjectJs().then((doc) => {
const body: HTMLBodyElement = document.getElementsByTagName('body')[0];
const customjs: HTMLScriptElement = document.createElement('script');
customjs.type = 'text/javascript';
customjs.id = 'customJsId'
customjs.src = doc.get('resourceUrl');
body.appendChild(customjs);
});
});
}
【问题讨论】:
-
窗口[“操纵器”]()
标签: javascript typescript dom react-tsx global-functions