在Angular 9中,默认情况下,HTML中的标签将被视为无效的标签。这是因为在Angular中,所有的JavaScript代码应该放在组件的逻辑中,并且由Angular编译器处理。
但是,如果你确实需要在HTML中使用标签,可以通过以下几种方法来解决这个问题:
方法一:使用Angular的Renderer2服务动态创建标签:
import { Component, OnInit, Renderer2 } from '@angular/core';
@Component({
  selector: 'app-my-component',
  template: ``,
})
export class MyComponent implements OnInit {
  constructor(private renderer: Renderer2) {}
  ngOnInit() {
    const script = this.renderer.createElement('script');
    script.type = 'text/javascript';
    script.src = 'path/to/your/script.js';
    this.renderer.appendChild(document.body, script);
  }
}
方法二:将JavaScript代码转移到组件的逻辑中,并在需要的地方使用标签嵌入:
import { Component } from '@angular/core';
@Component({
  selector: 'app-my-component',
  template: `
    
      
        
       
    
  `,
})
export class MyComponent {}
通过使用#scriptTemplate引用,你可以在组件的逻辑中通过ViewChild访问和操作这段代码。
这些方法可以让你在Angular 9中使用标签。但是请注意,在使用标签时要小心,确保遵循Angular的最佳实践,并确保代码的安全性。