【问题标题】:Typescript error TS2339: Property 'webkitURL' does not exist on type 'Window'打字稿错误 TS2339:“窗口”类型上不存在属性“webkitURL”
【发布时间】:2016-12-12 15:11:20
【问题描述】:

在使用 typescript 编译的项目上使用 Angular 2。

尝试创建 blob 图像时出现此错误:

error TS2339: Property 'webkitURL' does not exist on type 'Window'

ts 代码为:

public url = window.URL || window.webkitURL; this.photo = this.url.createObjectURL( res );

【问题讨论】:

  • webkitURL 已弃用。

标签: javascript google-chrome typescript angular webkit


【解决方案1】:

错误 TS2339:“窗口”类型上不存在属性“webkitURL”

lib.d.ts 不附带特定于浏览器的内容。但是你可以很容易地做到(window as any).webkitURL。这称为type assertion

更多

常见的(as any)风格类型断言是alm提供的快速修复:https://basarat.gitbooks.io/alm/content/features/quickfix.html

【讨论】:

    【解决方案2】:

    从 TypeScript 2.1.5 开始工作的解决方案:

    interface Window {
        webkitURL?: any;
    }
    
    declare var window: Window;
    
    if (window.webkitURL !== undefined) {
        console.log(window.webkitURL);
    }
    

    在上面的代码中,我们为 Window 声明了一个接口/形状,它可以选择定义 webkitURL,然后我们进行检查以确保它已定义。

    【讨论】:

      【解决方案3】:

      这种方法对我有用。我当前的 typescript 版本是 2.0.3

      把这个从类中添加出来

       interface Window { logged_user: Object }
      

      当你需要使用这个属性时,就使用它

      window.logged_user = {};//your data
      

      【讨论】:

        【解决方案4】:

        解决方案一:

        interface Window {
          webkitURL: any;
        }
        
        declare var window: Window;
        
         //Now typescript will not throw any error on window.webkitURL
        

        解决方案 2: (<any>window).webkitURL 不抛出 ts 错误

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-08-11
          • 2018-03-15
          • 2018-02-07
          • 2020-12-18
          • 2018-08-02
          • 2017-03-26
          • 2020-11-10
          相关资源
          最近更新 更多