打字稿:window.open().document.write() 是 'possible null '
type script : window.open().document.write() is 'possible null '
我在 Vue 和 Typescript
中编码
我的 Vue 组件中有这段代码
const WinPrint = window.open(.,.,.)
WinPrint.document.write(`<!DOCTYPE html> ....`)
但是我有这个错误
我该如何解决这个问题?
window.open
can return 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
分支中定义的。
我在 Vue 和 Typescript
中编码我的 Vue 组件中有这段代码
const WinPrint = window.open(.,.,.)
WinPrint.document.write(`<!DOCTYPE html> ....`)
但是我有这个错误
我该如何解决这个问题?
window.open
can return 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
分支中定义的。