angular - TypeScript declare var error for object.property - what to do?
问题描述
There are many ways suggested in SO to dynamically import external scripts such as 'https://something.com/...js' The problem is that TypeScript doesn't know about the properties so it gives errors.
As a work-around TS provides
declare var someObject
and you can put the object you retrieved from the external source in that. This works, no errors. However, it doesn't work for
let variable = someObject.property
The property gives an error because TS has no idea what is in your object.
Does anyone have this solved?
This is some of my actual code and it doesn't work. The error is
Property createUploadWidget does not exist on type 'Scripts'
interface Scripts {
name: string;
src: string;
}
declare var CloudinaryScripts: Scripts;
CloudinaryScripts =
{name: 'upLoader', src: 'https://widget.cloudinary.com/v2.0/global/all.js'};
export class CloudinaryComponent {
...
private uploadWidget = CloudinaryScripts.createUploadWidget(
{
cloudName: ...
}
Also, this creates a big error:
CloudinaryScripts[createUploadWidget]
解决方案
您可以通过将其定义为 any 来解决该错误
declare var someObject: any
你得到的错误是正确的
“脚本”类型上不存在属性 createUploadWidget(仅存在名称和 src)
interface Scripts {
name: string;
src: string;
}
推荐阅读
- mysql - 在间接相关表中的项目之间创建任意 1-1 对应关系
- jekyll - 将 Jekyll Liquid 变量(字符串)转换为 _data 文件路径
- winapi - 对文件进行“系统压缩”(CompactOS)的 Win32 API 是什么?
- java - 直方图中最大的矩形:为什么这段代码的大 O 是 O(nlog(n))?
- python - 将多词字符串拆分并收集到numpy数组中
- java - mp3player\MainApplication.java:29:错误:找不到符号导入 lrandomdev.com.online.mp3player.models.MyObjectBox;
- python - 在模型中添加项目
- reactjs - React:获取调用的 then 函数未被调用
- maven - Maven 执行器插件未执行
- python - 我有一个关于 MIMEMultipart 的问题