【问题标题】:Property 'contentDocument' does not exist on type 'HTMLElement'“HTMLElement”类型上不存在属性“contentDocument”
【发布时间】:2019-09-16 09:19:52
【问题描述】:

我正在使用 Object 元素加载 svg 文件,并且尝试从 ts 文件读取和修改 svg(例如动态着色并创建一些 svg 元素并附加到现有的 svg),但我遇到了以下错误: “HTMLElement”类型上不存在属性“contentDocument”

我正在使用 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>

在 app.component.ts 中

let obj=document.getElementById('svg1');
    let svg2=obj.contentDocument.querySelector("svg");
 let pt = svg2.createSVGPoint();

实际上它在“contentDocument”处显示错误(红线),但是当我运行应用程序时它工作正常,即使存在错误。 但是当我构建它时会出现问题。

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

【问题讨论】:

    标签: angular svg angular7


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2022-01-13
      • 2021-12-15
      • 2017-08-08
      • 2018-10-19
      • 2019-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多