我们想在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).


当前回答

不管你使用的是哪种JS框架,通过以下2步将bootstrap导入到你的项目很容易:

使用npm i -S bootstrap或yarn add bootstrap安装bootstrap。

在styles.css或styles中。导入bootstrap为@import ` ~bootstrap/dist/css/bootstrap.min.css `。

其他回答

如果你刚刚开始使用angular和bootstrap,那么简单的答案将如下步骤: 1. 添加节点包引导作为开发依赖

npm install --save bootstrap

在angular中导入引导样式表。json文件。 “风格”:( “项目/ app / src / styles.scss”, ”。/ node_modules /引导/ dist / css / bootstrap.min.css” ), 你已经准备好使用bootstrap进行样式化、网格化等。 >我的第一个bootstrap div</div>

我发现最好的部分是我们使用引导,没有任何第三方模块依赖。只要更新npm install——save bootstrap@4.4等命令,我们就可以随时升级引导程序。

更新v1.0.0-beta.26

你可以在文档中看到导入引导的新方法

如果仍然不工作,使用ng serve命令重新启动

1.0.0或以下版本:

在index.html文件中,你只需要引导css链接(不需要js脚本)

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

And

npm install ng2-bootstrap --save
npm install moment --save

在my_project/src/system-config中安装ng2-bootstrap后。ts:

/** Map relative paths to URLs. */
const map: any = {
  'moment': 'vendor/moment/moment.js',
  'ng2-bootstrap': 'vendor/ng2-bootstrap'
};

/** User packages configuration. */
const packages: any = {
  'ng2-bootstrap': {
    defaultExtension: 'js'
  },
  'moment':{
     format: 'cjs'
  }
};

在my_project/angular-cli-build.js中:

module.exports = function (defaults) {
return new Angular2App(defaults, {
    vendorNpmFiles: [
        'ng2-bootstrap/**/*',
        'moment/moment.js'
    ]
});
};

不要忘记这个命令 把你的模块放到供应商中:

ng build

从另一个模块导入相同的原理。

在项目中运行以下命令

npm install --save @bootsrap@4

如果你得到这样的确认

+ bootstrap@4.1.3
updated 1 package in 8.245s

说明boostrap 4安装成功。然而,为了使用它,你需要更新angular. properties下的“styles”数组。json文件。

按以下方式更新它,以便引导能够覆盖现有的样式

"styles": [
          "node_modules/bootstrap/dist/css/bootstrap.min.css",
           "src/styles.css"
          ],

为了确保一切都正确设置,运行ng serve >在http://localhost:4200/或一个端口上打开浏览器,运行angular应用>,右键单击>,检查>在头部检查样式,如果你有如下所示的引导,那么你可以使用boostrap。

使用angular-cli和css的工作示例:

1.安装引导

NPM安装bootstrap tether jquery——保存

2.添加到.angular-cli.json

  "styles": [
    "../node_modules/bootstrap/dist/css/bootstrap.css"
  ],
  "scripts": [
    "../node_modules/jquery/dist/jquery.js",
    "../node_modules/tether/dist/js/tether.js",
    "../node_modules/bootstrap/dist/js/bootstrap.js"
  ],

安装助推器

NPM install -save bootstrap

然后我们需要导入Bootstrap Sass到我们的应用程序中。在风格。SCSS添加这一行:

@ import“. . / node_modules /引导/ scss / bootstrap.scss”;

欲了解更多信息…