在我的angular cli 项目中,我需要引用不是节点包的外部js 库。因此,它们不会位于 node_modules 文件夹下,因为它们不是由 npm 安装的。这些是松散的 js 文件。
- 在 angular cli 项目结构中,我应该将它们添加到哪里才能最好 练习以便我可以将它们添加到脚本数组中 .angular-cli.json 文件?
- 如何在我的 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/