这个问题在这里已有答案:
我有一个JS / jQuery函数的文件,并希望在我的Angular 5项目的所有组件中使用它。那么,我如何在我的项目中使用它的功能。
注意:我已经安装了jQuery,并且可以在我的打字稿中使用它。但我想在typescript中使用我自己的JS / jQuery文件(app.js)中的函数。我怎样才能做到这一点?
你可以将它们包含在文件资产(或其他地方)中,并进一步将它们添加到.angular-cli.json
中,路径应该是正确的。您可以在资产中添加名为js的文件夹,并将此类文件放在那里。并修改文件.angular-cli.json
之类的
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"project": {
"name": "angular-test"
},
"apps": [
{
"root": "src",
"outDir": "dist",
"assets": [
"assets",
".htaccess",
"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": [
"../node_modules/font-awesome/css/font-awesome.min.css",
"assets/css/styles.min.css",
"styles.css"
],
"scripts": [
"assets/js/app.js" /* add all such files HERE */
],
"environmentSource": "environments/env.ts",
"environments": {
"dev": "environments/env.ts"
}
}
],
...
}