vue3+typescript项目中读取window对象中属性报错

问题

在vue3+ts开发的项目中,获取挂载在window对象上的属性a报错


image.png

解决方法

  1. 使用类型断言
    缺点:在 any 类型的变量上,访问任何属性都是允许的。它极有可能掩盖了真正的类型错误,所以如果不是非常确定,就不要使用 as any。
(window as any).a = 1;
  1. 类型申明
    src根目录*.d.ts文件中进行类型申明
interface Window {
    a: any;
}

你可能感兴趣的:(vue3+typescript项目中读取window对象中属性报错)