我已经尝试在angular.json
包中添加样式依赖,但显示模块未找到。添加了两个bootstrap文件。以下是两个文件的屏幕截图:
angular.json文件如下angular.json file
我已经尝试在angular.json
包中添加样式依赖,但显示模块未找到。添加了两个bootstrap文件。以下是两个文件的屏幕截图:
angular.json文件如下angular.json file
配置
在您的 angular.json 配置中,样式和脚本选项现在允许直接引用包:
之前:"styles": ["../node_modules/bootstrap/dist/css/bootstrap.css"]
之后:"styles": ["bootstrap/dist/css/bootstrap.css"]
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/ng6",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css","bootstrap/dist/css/bootstrap.min.css"
],
"scripts": [
"jquery/dist/jquery.min.js",
"bootstrap/dist/js/bootstrap.min.js"
]
},
Angular v6 及更新版本
从 Angular 6 开始,CLI 项目将使用 angular.json
代替 .angular-cli.json
用于构建和项目配置。
每个 CLI 工作区都有项目,每个项目都有目标,每个目标可以有配置。文档
. {
"projects": {
"my-project-name": {
"projectType": "application",
"architect": {
"build": {
"configurations": {
"production": {},
"demo": {},
"staging": {},
}
},
"serve": {},
"extract-i18n": {},
"test": {},
}
},
"my-project-name-e2e": {}
},
}
OPTION-1
执行 npm install bootstrap@4 jquery --save
Bootstrap
的 JavaScript 部分依赖于 jQuery
。因此你还需要 jQuery
的 JavaScript
库文件。
在你的 angular.json 文件中,在 build
目标下的样式和脚本数组中添加文件路径。
注意:
在 v6 之前,Angular CLI 项目配置存储在 <PATH_TO_PROJECT>/.angular-cli.json
中。自 v6 开始,该文件的位置改为 angular.json
。由于不再有前导点,所以该文件默认不再隐藏,并且与同级别在一起。
这也意味着 angular.json 中的文件路径不应包含前导点和斜杠。
即可以提供绝对路径而非相对路径。
在 .angular-cli.json
文件中,路径为 "../node_modules/"
在 angular.json
中,路径为 "node_modules/"
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/ng6",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css","node_modules/bootstrap/dist/css/bootstrap.min.css"
],
"scripts": ["node_modules/jquery/dist/jquery.min.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js"]
},
选项2
从CDN(内容分发网络)中添加文件到您的项目中CDN链接
打开文件 src/index.html 并插入:
<link>
元素以包含 Bootstrap CSS 文件<script>
元素以包含 jQuery<script>
元素以包含 Popper.js<script>
元素以包含 Bootstrap JavaScript 文件 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Angular</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<app-root>Loading...</app-root>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
选项3
执行npm install bootstrap
在src/styles.css
中添加以下内容:
@import "~bootstrap/dist/css/bootstrap.css";
选项4
ng-bootstrap是一组基于Bootstrap标记和CSS的本地Angular指令。因此,它不依赖于jQuery或Bootstrap的JavaScript
npm install --save @ng-bootstrap/ng-bootstrap
安装后,在您的根模块中导入并在@NgModule
的imports数组中注册它。
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
@NgModule({
declarations: [AppComponent, ...],
imports: [NgbModule.forRoot(), ...],
bootstrap: [AppComponent]
})
注意
ng-bootstrap
需要在你的项目中添加Bootstrap 4的css文件。你需要通过以下命令显式地安装它:
npm install bootstrap@4 --save
在你的angular.json中,在build
目标下的styles数组中添加文件路径即可。
"styles": [
"src/styles.css",
"node_modules/bootstrap/dist/css/bootstrap.min.css"
],
npm install --save bootstrap
之后,在项目根目录下的angular.json
(之前为.angular-cli.json
)中,找到styles
并像这样添加 Bootstrap CSS 文件:
适用于 Angular 6。
"styles": [
"../node_modules/bootstrap/dist/css/bootstrap.min.css",
"styles.css"
],
适用于 Angular 7
"styles": [
"node_modules/bootstrap/dist/css/bootstrap.min.css",
"src/styles.css"
],
npm install bootstrap --save
并将相关文件添加到 angular.json
文件中的 style
属性下,用于 CSS 文件,而 JS 文件则添加到 scripts
下。
"styles": [
"../node_modules/bootstrap/dist/css/bootstrap.min.css",
....
]
--save-dev
和另一个可选依赖项仍需要显式标志。 - AD8使用命令
npm install bootstrap --save
打开.angular.json文件,如果是旧版本则打开(.angular-cli.json)文件,找到"styles"字段并添加Bootstrap CSS文件。
"styles": [
"src/styles.scss",
"node_modules/bootstrap/dist/css/bootstrap.min.css"
],