簡體   English   中英

打字稿錯誤TS2339:'Window'類型中不存在屬性'webkitURL'

[英]Typescript error TS2339: Property 'webkitURL' does not exist on type 'Window'

在使用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 );

錯誤TS2339:類型'Window'上不存在屬性'webkitURL'

lib.d.ts不附帶瀏覽器特定的內容。 但是您可以輕松地執行(window as any).webkitURL 這稱為類型斷言

更多

常見(as any)樣式類型斷言是由alm提供的quickfix: https ://basarat.gitbooks.io/alm/content/features/quickfix.html

從TypeScript 2.1.5開始工作的解決方案:

interface Window {
    webkitURL?: any;
}

declare var window: Window;

if (window.webkitURL !== undefined) {
    console.log(window.webkitURL);
}

在上面的代碼中,我們聲明了一個Window的接口/形狀,它可以選擇定義webkitURL,然后我們進行檢查以確保定義它。

這種方法對我有用。 我當前的打字稿版本是2.0.3

將其添加到課堂之外

 interface Window { logged_user: Object }

當你需要使用這個屬性時,只需使用它

window.logged_user = {};//your data

解決方案1:

interface Window {
  webkitURL: any;
}

declare var window: Window;

 //Now typescript will not throw any error on window.webkitURL

解決方案2: (<any>window).webkitURL不會拋出ts錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM