[英]Typescript error TS2339: Property 'webkitURL' does not exist on type 'Window'
Using Angular 2 on a project which is compiled with typescript. 在使用typescript编译的项目上使用Angular 2。
Getting this error when trying to create a blob image: 尝试创建blob图像时出现此错误:
error TS2339: Property 'webkitURL' does not exist on type 'Window'
ts code is: ts代码是:
public url = window.URL || window.webkitURL; this.photo = this.url.createObjectURL( res );
error TS2339: Property 'webkitURL' does not exist on type 'Window'
错误TS2339:类型'Window'上不存在属性'webkitURL'
The lib.d.ts does not ship with stuff that is browser specific. lib.d.ts不附带浏览器特定的内容。 However you can easily do
(window as any).webkitURL
. 但是您可以轻松地执行
(window as any).webkitURL
。 This is called a type assertion . 这称为类型断言 。
The common (as any)
style type assertion is a quickfix provided by alm : https://basarat.gitbooks.io/alm/content/features/quickfix.html 常见
(as any)
样式类型断言是由alm提供的quickfix: https ://basarat.gitbooks.io/alm/content/features/quickfix.html
The solution that works as of TypeScript 2.1.5: 从TypeScript 2.1.5开始工作的解决方案:
interface Window {
webkitURL?: any;
}
declare var window: Window;
if (window.webkitURL !== undefined) {
console.log(window.webkitURL);
}
In the above code we declared an interface/shape for Window which will optionally have webkitURL defined and then we do a check to make sure it is defined. 在上面的代码中,我们声明了一个Window的接口/形状,它可以选择定义webkitURL,然后我们进行检查以确保定义它。
This approach worked for me. 这种方法对我有用。 My curren version of typescript is 2.0.3
我当前的打字稿版本是2.0.3
add this out of the class 将其添加到课堂之外
interface Window { logged_user: Object }
and when you need use this property, just use it 当你需要使用这个属性时,只需使用它
window.logged_user = {};//your data
Solution 1: 解决方案1:
interface Window {
webkitURL: any;
}
declare var window: Window;
//Now typescript will not throw any error on window.webkitURL
Solution 2: (<any>window).webkitURL
does not throw ts error 解决方案2:
(<any>window).webkitURL
不会抛出ts错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.