手动包含 Jquery 文件时无法在 Angular 9 中找到 jquery 文件
Cannot find jquery file in Angular 9 when include the Jquery file manually
我尝试将 jquery 库文件添加到 assets 文件夹,并将该目录添加到 angular.json 中的脚本,但是当我 运行 ng serve
Angular告诉我 Jquery 库不存在,我正在使用 angular 版本 9。
不知道问题出在哪里,请大家帮忙
这是错误信息:
An unhandled exception occurred: Script file assets/js/jquery-3.5.1.slim.min.js does not exist.
angular.json 文件:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"HorsesClubFrontend": {
"projectType": "application",
"schematics": {},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/HorsesClubFrontend",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"aot": true,
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css",
"assets/css/bootstrap.min.css"
],
"scripts": [
"assets/js/jquery-3.5.1.slim.min.js",
"assets/js/bootstrap.min.js",
"assets/js/popper.min.js"]
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "6kb",
"maximumError": "10kb"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "HorsesClubFrontend:build"
},
"configurations": {
"production": {
"browserTarget": "HorsesClubFrontend:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "HorsesClubFrontend:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css"
],
"scripts": []
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"tsconfig.app.json",
"tsconfig.spec.json",
"e2e/tsconfig.json"
],
"exclude": [
"**/node_modules/**"
]
}
},
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "HorsesClubFrontend:serve"
},
"configurations": {
"production": {
"devServerTarget": "HorsesClubFrontend:serve:production"
}
}
}
}
}},
"defaultProject": "HorsesClubFrontend"
}
资产文件夹:
要在 angular 中添加 jquery 必须按照以下步骤操作。
步骤1
Add jquery file in index.html file into the head tag
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
第2步
Also need to install two npm packages
npm i jquery@3.5.1
npm i @types/jquery
第三步
Add jquery into angular.json file into the script array
"scripts": [
"node_modules/jquery/dist/jquery.js"
]
尝试以上步骤,希望对您有所帮助。
感谢您的回答,您的工作方式,但我找到了其他方法,直接安装 bootstrap npm install bootstrap --save
并将库包含到 angular.json:
"styles": [
"./node_modules/bootstrap/dist/css/bootstrap.css",
"src/styles.css"
],
"scripts": [
"./node_modules/jquery/dist/jquery.js",
"./node_modules/bootstrap/dist/js/bootstrap.js"
]
而且效果很好
我尝试将 jquery 库文件添加到 assets 文件夹,并将该目录添加到 angular.json 中的脚本,但是当我 运行 ng serve
Angular告诉我 Jquery 库不存在,我正在使用 angular 版本 9。
不知道问题出在哪里,请大家帮忙
这是错误信息:
An unhandled exception occurred: Script file assets/js/jquery-3.5.1.slim.min.js does not exist.
angular.json 文件:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"HorsesClubFrontend": {
"projectType": "application",
"schematics": {},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/HorsesClubFrontend",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"aot": true,
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css",
"assets/css/bootstrap.min.css"
],
"scripts": [
"assets/js/jquery-3.5.1.slim.min.js",
"assets/js/bootstrap.min.js",
"assets/js/popper.min.js"]
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "6kb",
"maximumError": "10kb"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "HorsesClubFrontend:build"
},
"configurations": {
"production": {
"browserTarget": "HorsesClubFrontend:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "HorsesClubFrontend:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css"
],
"scripts": []
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"tsconfig.app.json",
"tsconfig.spec.json",
"e2e/tsconfig.json"
],
"exclude": [
"**/node_modules/**"
]
}
},
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "HorsesClubFrontend:serve"
},
"configurations": {
"production": {
"devServerTarget": "HorsesClubFrontend:serve:production"
}
}
}
}
}},
"defaultProject": "HorsesClubFrontend"
}
资产文件夹:
要在 angular 中添加 jquery 必须按照以下步骤操作。
步骤1Add jquery file in index.html file into the head tag
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
第2步
Also need to install two npm packages
npm i jquery@3.5.1
npm i @types/jquery
第三步
Add jquery into angular.json file into the script array
"scripts": [
"node_modules/jquery/dist/jquery.js"
]
尝试以上步骤,希望对您有所帮助。
感谢您的回答,您的工作方式,但我找到了其他方法,直接安装 bootstrap npm install bootstrap --save
并将库包含到 angular.json:
"styles": [
"./node_modules/bootstrap/dist/css/bootstrap.css",
"src/styles.css"
],
"scripts": [
"./node_modules/jquery/dist/jquery.js",
"./node_modules/bootstrap/dist/js/bootstrap.js"
]
而且效果很好