【问题标题】:Where to add or place external Javascript files in angular cli project structure and use them in typescript file?在 Angular cli 项目结构中添加或放置外部 Javascript 文件并在 typescript 文件中使用它们?
【发布时间】:2018-02-24 15:22:46
【问题描述】:

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

  1. 在 Angular cli 项目结构中,我应该在哪里添加它们作为最佳 练习,以便我可以将它们添加到脚本数组中 .angular-cli.json 文件?
  2. 如何在我的打字稿文件中使用它们?

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

【问题讨论】:

标签: angular angular-cli


【解决方案1】:

在资产文件夹中添加您的外部 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 {

}

【讨论】:

  • 我又添加了一个问题 - 如何在我的打字稿文件中使用它们?你也可以帮忙吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-13
  • 1970-01-01
  • 2020-11-01
  • 2018-04-03
  • 2016-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多