-1

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

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

4

1 回答 1

1

你可以试试

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');
}

请参考这个答案:Angular 2: import external js file into component

于 2020-06-29T05:59:22.633 回答