如何在 Angular CLI 组件中添加仅特定于该组件的外部 JS 文件?

How can I add external JS file in Angular CLI component, specific to the component only?

我在 Angular CLI 应用程序中有主页和关于组件。我已将两个组件所需的 javascript 文件包含在 angular.json 中。但是我想在组件中添加特定于组件的 javascript 文件。比如说,我想将 home.js 添加到 Home 组件。有什么办法吗?

我尝试使用此 将脚本添加到 DOM,但是,我想改为添加外部 js 文件。

你可以试试

import * as test from 'test.js';

export class HomeComponent {
    ngOnInit() {
       test.testFunction(); //testFunction is in home.js
   }
}

// test.js
export function testFunction() {
    console.log('hello world');
}

请参考这个回答: