【发布时间】:2021-12-16 19:46:29
【问题描述】:
我在 Vue 和 Typescript
中编码我的 Vue 组件中有这段代码
const WinPrint = window.open(.,.,.)
WinPrint.document.write(`<!DOCTYPE html> ....`)
但我有这个错误
我该如何解决这个问题?
【问题讨论】:
标签: typescript vue.js nuxt.js
我在 Vue 和 Typescript
中编码我的 Vue 组件中有这段代码
const WinPrint = window.open(.,.,.)
WinPrint.document.write(`<!DOCTYPE html> ....`)
但我有这个错误
我该如何解决这个问题?
【问题讨论】:
标签: typescript vue.js nuxt.js
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 分支中定义的。
【讨论】: