我正在开发一个MEAN应用程序,我的文件夹结构如下:
我在/ angular / src / assets / js下添加了一个js文件,用于一些jQuery的东西 .
为此,我使用npm安装了jQuery .
现在我需要通过 angular.cli.json 文件调用该js文件 .
angular.cli.json:
"apps": [
{
"root": "angular/src",
"outDir": "angular/dist",
"assets": [
"assets",
"favicon.ico"
],
"index": "index.html",
"main": "main.ts",
"polyfills": "polyfills.ts",
"test": "test.ts",
"tsconfig": "tsconfig.app.json",
"testTsconfig": "tsconfig.spec.json",
"prefix": "app",
"styles": [
"styles.css"
],
"scripts": [
"../node_modules/jquery/dist/jquery.min.js",
"../src/assets/js/custom.js"
],
由于我将根指向“angular / src”,因此存在将jquery.min.js包含在脚本部分中的问题,该部分位于“mean / node_modules /”下并以“未找到”错误结束 .
angular.cli.json文件在“angular / src”根路径中查找“node_modules / jquery / dist / jquery.min.js” .
有没有办法解决这个问题而不改变我的文件夹结构?
需要别人的宝贵帮助 .
2 回答
最简单的方法是直接在index.html中链接js文件 .
如果您希望通过angular编译和维护它,请检查此链接
How to include external js file in Angular 4 and call function from angular to js
https://www.thepolyglotdeveloper.com/2016/01/include-external-javascript-libraries-in-an-angular-2-typescript-project/
是 . 从angular.cli.json中删除脚本链接,然后将其包含在带脚本标记的索引html中 .
它工作正常 .