简体   繁体   English

Bootstrap 4无法与Angular CLI一起使用

[英]Bootstrap 4 not working with Angular CLI

I've installed Bootstrap 4 using node into an Angular 2 project by following this guide: 我已按照以下指南将使用node的Bootstrap 4安装到Angular 2项目中:

https://github.com/angular/angular-cli/wiki/stories-include-bootstrap https://github.com/angular/angular-cli/wiki/stories-include-bootstrap

Followed the instructions to the letter, but now when I try and run the application using ng serve I get compile errors. 按照这封信的说明进行操作,但是现在当我尝试使用ng serve运行应用程序时,出现编译错误。

ERROR in ./node_modules/css-loader?{"sourceMap":false,"importLoaders":1}!./node_modules/postcss-loader/lib?{"ident":"postcss","sourceMap":false}!./node_modules/bootstrap/dist/css/bootstrap.min.css
Module build failed: BrowserslistError: Unknown browser major
    at error (D:\GitKracken\KnightOwlUI2018\KOWebUi\node_modules\browserslist\index.js:37:11)
    at Function.browserslist.checkName (D:\GitKracken\KnightOwlUI2018\KOWebUi\node_modules\browserslist\index.js:320:18)
    at Function.select (D:\GitKracken\KnightOwlUI2018\KOWebUi\node_modules\browserslist\index.js:438:37)
    at D:\GitKracken\KnightOwlUI2018\KOWebUi\node_modules\browserslist\index.js:207:41
    at Array.forEach (native)
    at browserslist (D:\GitKracken\KnightOwlUI2018\KOWebUi\node_modules\browserslist\index.js:196:13)
    at Browsers.parse (D:\GitKracken\KnightOwlUI2018\KOWebUi\node_modules\autoprefixer\lib\browsers.js:44:14)
    at new Browsers (D:\GitKracken\KnightOwlUI2018\KOWebUi\node_modules\autoprefixer\lib\browsers.js:39:28)
    at loadPrefixes (D:\GitKracken\KnightOwlUI2018\KOWebUi\node_modules\autoprefixer\lib\autoprefixer.js:56:18)
    at plugin (D:\GitKracken\KnightOwlUI2018\KOWebUi\node_modules\autoprefixer\lib\autoprefixer.js:62:18)
    at LazyResult.run (D:\GitKracken\KnightOwlUI2018\KOWebUi\node_modules\postcss-loader\node_modules\postcss\lib\lazy-result.js:277:20)
    at LazyResult.asyncTick (D:\GitKracken\KnightOwlUI2018\KOWebUi\node_modules\postcss-loader\node_modules\postcss\lib\lazy-result.js:192:32)
    at LazyResult.asyncTick (D:\GitKracken\KnightOwlUI2018\KOWebUi\node_modules\postcss-loader\node_modules\postcss\lib\lazy-result.js:204:22)
    at LazyResult.asyncTick (D:\GitKracken\KnightOwlUI2018\KOWebUi\node_modules\postcss-loader\node_modules\postcss\lib\lazy-result.js:204:22)
    at processing.Promise.then._this2.processed (D:\GitKracken\KnightOwlUI2018\KOWebUi\node_modules\postcss-loader\node_modules\postcss\lib\lazy-result.js:231:20)
    at LazyResult.async (D:\GitKracken\KnightOwlUI2018\KOWebUi\node_modules\postcss-loader\node_modules\postcss\lib\lazy-result.js:228:27)
 @ ./node_modules/bootstrap/dist/css/bootstrap.min.css 4:14-131
 @ multi ./node_modules/bootstrap/dist/css/bootstrap.min.css ./src/styles.css

My apps section of my .angular-cli.json file (as far as I can see) seems ok and is the only file I changed since installing Bootstrap 4 and the physical bootstrap files are in the node modules folder. 我的.angular-cli.json文件的我的应用程序部分(据我所知)似乎正常,并且是自从安装Bootstrap 4和物理引导程序文件以来,我更改的唯一文件,该文件位于节点模块文件夹中。 My node_modules folder is in the root of the project on the same level as the src folder just as the cli sets it up by default. 我的node_modules文件夹与src文件夹位于同一级别的项目根目录中,就像cli在默认情况下对其进行设置一样。

"apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "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/bootstrap/dist/css/bootstrap.min.css",
        "styles.css"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],

Have I missed a step somewhere? 我在某个地方错过了一步吗? Or maybe the guide I'm ready is out of date, I'm just not sure, but this feels like it should be really simple and should just work? 或者也许我已经准备好的指南已经过时了,我不确定,但是感觉好像应该很简单并且应该可以工作吗?

Have you seen this bug report ? 您看到此错误报告了吗?

Basically, two main solutions I get from this: 基本上,我从中得到两个主要解决方案:

  1. Try with other minor versions of Bootstrap 尝试使用其他次要版本的Bootstrap
  2. Use Sass and import Bootstrap Sass files instead of adding Bootstrap minified CSS in angular-cli.json 使用Sass并导入Bootstrap Sass文件,而不是在angular-cli.json中添加Bootstrap缩小的CSS

Personally I copy the entire Sass code from Bootstrap Github tags into my project. 我个人将整个Sass代码从Bootstrap Github标记复制到我的项目中。 This gives me more flexibility. 这给了我更多的灵活性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM