angular - 在angular cli项目结构中的何处添加或放置外部Javascript文件并在 typescript 文件中使用它们?

标签 angular angular-cli

在我的angular cli 项目中,我需要引用不是节点包的外部js 库。因此,它们不会位于 node_modules 文件夹下,因为它们不是由 npm 安装的。这些是松散的 js 文件。

  1. 在 angular cli 项目结构中,我应该将它们添加到哪里才能最好 练习以便我可以将它们添加到脚本数组中 .angular-cli.json 文件?
  2. 如何在我的 typescript 文件中使用它们?

下面的链接解释了我们如何在 .angular-cli.json 文件的脚本数组中引用它们。但是,它没有说明如果外部 js 文件不是节点模块,应该将它们添加到 Angular cli 项目的文件夹结构中的什么位置。 Angular Cli Webpack, How to add or bundle external js files?

最佳答案

在 assets 文件夹中添加外部 js 文件,例如:assets=>js=>filename.js

.angular-cli.json

"scripts": [
    "../src/assets/js/externalfilename.js"
]

在 .angular-cli.json 文件更改后很重要。停止项目然后开始

它如何在下面的 ts 文件示例中使用:

editor.js

<!-- assets/js/editor.js -->
Editor = function() {
    //This is my javascript function
    console.log('Editor');
}

editor.js 会加入.angular-cli.json

"scripts": [
        "../src/assets/js/editor.js"
    ]

在component.ts文件中

declare var Editor: any;

export class EditorComponent {

}

关于angular - 在angular cli项目结构中的何处添加或放置外部Javascript文件并在 typescript 文件中使用它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46239482/

相关文章:

angular - 同时运行多个应用程序?

javascript - angular2使用http读取json文件

javascript - 如何将 Angular1 应用程序改造成 VueJs 应用程序

css - 使用 Mat Cards 时无法玩绝对/相对位置

javascript - SystemJS 对不存在的文件发出约 200 个请求

javascript - 在 Angular 中使用源映射生成生产版本 - CLI

javascript - 在 Angular 6 上为 *ngFor 内的输入类型复选框启用双向数据绑定(bind)

angular-cli - 如何从观察者文件中排除 node_modules

angular - 使用 Nginx 为 Angular2 CLI 构建的应用程序提供服务会抛出 404、403

node.js - 如何在可视代码中调试 Angular 7 库