将 Bootstrap 3 CSS 导入 Angular 2+ 并将 SASS 作为 CSS

Import Bootstrap 3 CSS into Angular 2+ with SASS as CSS

我需要覆盖 Bootstrap (3.3.7) CSS 中的某些内容,因此我需要在所有其他样式之前加载 Bootstrap 才能做到这一点。我知道这样做的方法是将它导入到文档顶部我的应用程序的主 styles.sass 中。但是当我通过以下方式执行此操作时:

@import ./bootstrap.css

我得到:

ERROR in ./~/css-loader!./src/bootstrap.css
Module not found: Error: Can't resolve '../fonts/glyphicons-halflings-regular.eot' in '/Users/bradley/Work/dashboard/src'
 @ ./~/css-loader!./src/bootstrap.css 6:4445-4497 6:4520-4572
 @ ./~/css-loader!./~/postcss-loader!./~/sass-loader!./src/styles.sass
 @ ./src/styles.sass
 @ multi ./src/styles.sass

ERROR in ./~/css-loader!./src/bootstrap.css
Module not found: Error: Can't resolve '../fonts/glyphicons-halflings-regular.woff2' in '/Users/bradley/Work/dashboard/src'
 @ ./~/css-loader!./src/bootstrap.css 6:4622-4676
 @ ./~/css-loader!./~/postcss-loader!./~/sass-loader!./src/styles.sass
 @ ./src/styles.sass
 @ multi ./src/styles.sass

ERROR in ./~/css-loader!./src/bootstrap.css
Module not found: Error: Can't resolve '../fonts/glyphicons-halflings-regular.woff' in '/Users/bradley/Work/dashboard/src'
 @ ./~/css-loader!./src/bootstrap.css 6:4707-4760
 @ ./~/css-loader!./~/postcss-loader!./~/sass-loader!./src/styles.sass
 @ ./src/styles.sass
 @ multi ./src/styles.sass

ERROR in ./~/css-loader!./src/bootstrap.css
Module not found: Error: Can't resolve '../fonts/glyphicons-halflings-regular.ttf' in '/Users/bradley/Work/dashboard/src'
 @ ./~/css-loader!./src/bootstrap.css 6:4790-4842
 @ ./~/css-loader!./~/postcss-loader!./~/sass-loader!./src/styles.sass
 @ ./src/styles.sass
 @ multi ./src/styles.sass

ERROR in ./~/css-loader!./src/bootstrap.css
Module not found: Error: Can't resolve '../fonts/glyphicons-halflings-regular.svg' in '/Users/bradley/Work/dashboard/src'
 @ ./~/css-loader!./src/bootstrap.css 6:4876-4928
 @ ./~/css-loader!./~/postcss-loader!./~/sass-loader!./src/styles.sass
 @ ./src/styles.sass
 @ multi ./src/styles.sass

那没有用,因为我没有将字体带入项目。

这让我觉得一定有更合适的安装方法 bootstrap。

然后我找到 bootstrap-sass,并使用 npm install bootstrap-sass --save 将其导入,然后将其导入 styles.sass

@import ../node_modules/bootstrap-sass/assets/stylesheets/_bootstrap.scss

然后我得到:

ERROR in ./~/css-loader!./~/postcss-loader!./~/sass-loader!./src/styles.sass
Module not found: Error: Can't resolve '../fonts/bootstrap/glyphicons-halflings-regular.eot' in '/Users/bradley/Work/dashboard/src'
 @ ./~/css-loader!./~/postcss-loader!./~/sass-loader!./src/styles.sass 6:4253-4315 6:4338-4400
 @ ./src/styles.sass
 @ multi ./src/styles.sass

ERROR in ./~/css-loader!./~/postcss-loader!./~/sass-loader!./src/styles.sass
Module not found: Error: Can't resolve '../fonts/bootstrap/glyphicons-halflings-regular.woff2' in '/Users/bradley/Work/dashboard/src'
 @ ./~/css-loader!./~/postcss-loader!./~/sass-loader!./src/styles.sass 6:4452-4516
 @ ./src/styles.sass
 @ multi ./src/styles.sass

ERROR in ./~/css-loader!./~/postcss-loader!./~/sass-loader!./src/styles.sass
Module not found: Error: Can't resolve '../fonts/bootstrap/glyphicons-halflings-regular.woff' in '/Users/bradley/Work/dashboard/src'
 @ ./~/css-loader!./~/postcss-loader!./~/sass-loader!./src/styles.sass 6:4549-4612
 @ ./src/styles.sass
 @ multi ./src/styles.sass

ERROR in ./~/css-loader!./~/postcss-loader!./~/sass-loader!./src/styles.sass
Module not found: Error: Can't resolve '../fonts/bootstrap/glyphicons-halflings-regular.ttf' in '/Users/bradley/Work/dashboard/src'
 @ ./~/css-loader!./~/postcss-loader!./~/sass-loader!./src/styles.sass 6:4644-4706
 @ ./src/styles.sass
 @ multi ./src/styles.sass

ERROR in ./~/css-loader!./~/postcss-loader!./~/sass-loader!./src/styles.sass
Module not found: Error: Can't resolve '../fonts/bootstrap/glyphicons-halflings-regular.svg' in '/Users/bradley/Work/dashboard/src'
 @ ./~/css-loader!./~/postcss-loader!./~/sass-loader!./src/styles.sass 6:4742-4804
 @ ./src/styles.sass
 @ multi ./src/styles.sass

难道不应该有一个干净的方法来做到这一点吗?

我假设你正在使用 webpack,所以你应该看看这个线程:

Bootstrap.sass and Webpack

我开始学习 Bootstrap 因为 Sass 有一个变量专门用于将它定向到字体所在的位置。如果它们是使用 npm install bootstrap-sass --save 安装的,那么您需要在 styles.sass:

的顶部使用这两行导入 bootstrap-sass
$icon-font-path: "~bootstrap-sass/assets/fonts/bootstrap/"
@import ../node_modules/bootstrap-sass/assets/stylesheets/_bootstrap.scss