角度:属性'xxx'在类型 'Window & typeof globalThis' 上不存在



我在为应用程序提供服务后,在命令行中收到添加的Trustbox组件ts文件的问题。

    "Property 'Trustpilot' does not exist on type 'Window & typeof globalThis'."

我试图宣布这一窗口,并四处寻找解决方案,但我一生都无法找到。

这是trustbox.component.ts代码

import { Component, OnInit } from '@angular/core';
@Component({
  selector: 'app-trustbox',
  templateUrl: './trustbox.component.html',
})
export class TrustboxComponent implements OnInit {
  constructor() {}
  ngOnInit() {
    const trustboxRef = document.getElementById('trustbox');
    window.Trustpilot.loadFromElement(trustboxRef);
  }
}

根据Trustpilot文章(https://support.trustpilot.com/hc/en-us/articles/115011421468--Add-a-TrustBox-widget-to-a-single-page-application)要让他们的小部件在SPA上工作,你必须

  1. 实现OnInit
  2. 获取对我们之前粘贴的元素的引用
  3. 调用loadFromElement函数

我知道这可能很简单,但它非常烦人,因为我无法编译我的应用程序。非常感谢您的帮助。提前感谢!

使用@ViewChild获取引用并对其进行处理,并使用AfterViewInit仅在读取器完成并在屏幕上创建组件后使用。

示例:

import { Component, ViewChild, AfterViewInit, ElementRef } from "@angular/core";
@Component({
  selector: "my-app",
  template: `
    <p #pRef>
      Start editing to see some magic happen :)
    </p>
  `,
  styleUrls: ["./app.component.css"]
})
export class AppComponent implements AfterViewInit {
  name = "Angular";
  @ViewChild("pRef", { static: false }) pRef: ElementRef;
  ngAfterViewInit() {
    console.log(this.pRef.nativeElement.innerHTML);
    this.pRef.nativeElement.innerHTML = "DOM updated succesfully!!!";
  }
}

相关内容

最新更新