将脚本子节点附加到特定的Angular组件中。
代码示例:
import { Component, ElementRef } from '@angular/core';
@Component({
selector: 'my-component',
template: ''
})
export class MyComponent {
constructor(private elRef: ElementRef) {}
ngOnInit() {
const script = document.createElement('script');
script.src = 'path/to/script.js';
this.elRef.nativeElement.appendChild(script);
}
}
在组件的ngOnInit方法中,创建一个新脚本元素,并将其附加到示例DOM元素。
在脚本元素的src属性中指定要加载的脚本文件的路径。
将新创建的脚本元素附加到示例DOM元素上,以便脚本文件可以加载并运行。