我正在用Vue和Typescript編碼
我的 Vue 組件中有此代碼
const WinPrint = window.open(.,.,.)
WinPrint.document.write(`<!DOCTYPE html> ....`)
但我有這個錯誤

我該如何解決這個問題?
uj5u.com熱心網友回復:
window.open 在某些情況下可以回傳 null。通過您提到的錯誤,TypeScript 警告您可能正在訪問null那里的某些屬性。您需要檢查null代碼中的大小寫。就像是
const WinPrint = window.open(.,.,.);
if (!WinPrint) {
// handle the case when the window could not be opened here
} else {
WinPrint.document.write(`<!DOCTYPE html> ....`)
}
這將使 TypeScript 看到WinPrint是在else分支中定義的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/344804.html
上一篇:為什么我不能在休息時除錯;行以及為什么串列包含空專案?
下一篇:打字稿:如何定義物件屬性的型別?
