类型“HTMLElement”上不存在属性“contentDocument”

Ram*_*han 5 svg angular angular7

我正在使用元素加载 svg 文件Object,并从 TypeScript 文件中尝试读取和修改 svg(例如动态着色、创建一些 svg 元素以及附加到现有 svg),但遇到以下错误:

Property 'contentDocument' does not exist on type 'HTMLElement'

我正在使用 Angular 7。

在app.component.html中

 <object id="svg1" data="assets/10026_019.svg" type="image/svg+xml" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"></object>
Run Code Online (Sandbox Code Playgroud)

在 app.component.ts 中

let obj=document.getElementById('svg1');
    let svg2=obj.contentDocument.querySelector("svg");
 let pt = svg2.createSVGPoint();
Run Code Online (Sandbox Code Playgroud)

实际上它在 处显示错误(红线)contentDocument,但是当我运行该应用程序时,即使报告了错误,它也工作正常。

但当我这样做时ng build,它报告了这个问题。

我不确定如何纠正此错误以使构建成功。

Ram*_*han 6

我通过更新如下自行解决了这个问题。我已经明确提到对象类型为“any”。

let obj: any =document.getElementById('svg1');
    let svg2: any =obj.contentDocument.querySelector("svg");
 let pt = svg2.createSVGPoint();
Run Code Online (Sandbox Code Playgroud)