【问题标题】:type script : window.open().document.write() is 'possible null '打字稿:window.open().document.write() 是“可能为空”
【发布时间】:2021-12-16 19:46:29
【问题描述】:

我在 VueTypescript

中编码

我的 Vue 组件中有这段代码

const WinPrint = window.open(.,.,.) 

WinPrint.document.write(`<!DOCTYPE html> ....`)

但我有这个错误

我该如何解决这个问题?

【问题讨论】:

    标签: typescript vue.js nuxt.js


    【解决方案1】:

    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 分支中定义的。

    【讨论】:

      猜你喜欢
      • 2017-10-12
      • 1970-01-01
      • 2023-01-20
      • 2022-01-26
      • 1970-01-01
      • 2020-11-07
      • 2021-12-01
      • 2021-09-27
      • 2021-08-09
      相关资源
      最近更新 更多