我们想在angular-cli 1.0.0-beta.5生成的应用程序中使用bootstrap 4 (4.0.0-alpha.2)(w/ node v6.1.0)。

在获得bootstrap及其对npm的依赖之后,我们的第一个方法是将它们添加到angular-cli-build.js中:

'bootstrap/dist/**/*.min.+(js|css)',  
'jquery/dist/jquery.min.+(js|map)',  
'tether/dist/**/*.min.+(js|css)',

然后导入到index。html中

<script src="vendor/jquery/dist/jquery.min.js"></script>
<script src="vendor/tether/dist/js/tether.min.js"></script>
<link rel="stylesheet" type="text/css" href="vendor/bootstrap/dist/css/bootstrap.min.css">
<script src="vendor/bootstrap/dist/js/bootstrap.min.js"></script>

这在ng serve中工作得很好,但当我们生成一个带有-prod标志的构建时,dist/vendor中的所有依赖项都消失了(惊讶!)

我们打算如何处理这样的场景(即加载引导脚本)在一个用angular-cli生成的项目?

我们有以下想法,但我们真的不知道该怎么做……

use a CDN ? but we would rather serve these files to guarantee that they will be available copy dependencies to dist/vendor after our ng build -prod ? But that seems like something angular-cli should provide since it 'takes care' of the build part ? adding jquery, bootstrap and tether in src/system-config.ts and somehow pull them into our bundle in main.ts ? But that seemed wrong considering that we are not going to explicitly use them in our application's code (unlike moment.js or something like lodash, for example).


当前回答

Angular 5中引导4的步骤

创建Angular 5项目 ng新的AngularBootstrapTest 移动到项目目录 cd AngularBootstrapTest 下载引导 NPM安装引导 添加引导到项目 4.1打开.angular-cli.json 4.2在json中找到“styles”部分 4.3添加bootstrap CSS路径如下所示 . “风格”:( “. . / node_modules /引导/ dist / css / bootstrap.min.css”, “styles”css ),

现在你已经成功地在angular 5项目中添加了bootstrap css

测试引导

src / app / app.component.html开放 添加引导按钮组件

.

<button type="button" class="btn btn-primary">Primary</button>

你可以参考按钮的样式在这里(https://getbootstrap.com/docs/4.0/components/buttons/)

保存文件 运行项目 发球——开球

现在,您将在页面中看到引导样式按钮

注意: 如果你在ng serve之后添加了引导路径,你必须停止并重新运行ng serve来反映这些变化

其他回答

添加Bootstrap和Jquery

npm install bootstrap@3 jquery --save

运行此命令后,请继续检查您的node_modules文件夹,您应该能够看到bootstrap和jquery添加到其中。

Angular-cli现在有一个专门的wiki页面,在那里你可以找到你需要的一切。TLDR,通过npm安装bootstrap,并添加样式链接到你的。angular-cli中的“styles”部分。json文件

在bash中运行npm install ng2-bootstrap bootstrap——save 在angular-cli.json中添加/更改以下内容 “风格”:( “. . / node_modules /引导/ dist / css / bootstrap.min.css” ), “脚本”:( “. . / node_modules / jquery / dist / jquery.min.js”, “. . / node_modules /引导/ dist / js / bootstrap.min.js” ),

2个简单步骤


安装引导程序(正在安装最新版本)


npm install bootstrap@next

导入到你的项目中,在你的styles.scss中添加下面这行


@import '~bootstrap';

请注意您的导入顺序可能会有影响,如果您有其他库使用bootstrap,请将此import语句放在顶部

最后。:)


注:以下是我的版本


Angular: 9

节点:v10.16.0

NPM: 6.9.1


步骤1: NPM安装bootstrap@latest——save-dev 这将安装最新版本的引导程序, 但是,可以通过添加版本号来安装指定的版本

打开styles.css并添加以下内容 @ import " ~引导/ dist / css / bootstrap.css”