angular2 - bootstrap-sass 使用 angular-cli 的编译错误

angular2 - bootstrap-sass compilation error using angular-cli

我正在使用 angular-cli: 1.0.0-beta.19-3node: v5.11.1

在我的angular-cli.json文件中,我将全局样式文件扩展名设置为.scss。

这是我的样式文件。

angular2/src/styles.scss

// Default Font
@import url(https://fonts.googleapis.com/css?family=Raleway:300,400,600);

// Typography
$font-family-sans-serif: "Raleway", sans-serif;
$font-size-base: 14px;
$line-height-base: 1.6;
$text-color: #636b6f;

// Import bootstrap
@import "node_modules/bootstrap-sass/assets/stylesheets/bootstrap";

当我尝试导入 bootrap-sass 模块时出现错误,它给出了与 glyphicon 图标相关的错误。不知道怎么解决。

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

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

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

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

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

经过一些研究后,我在这里找到了解决方案。

https://github.com/angular/angular-cli/issues/2170

我必须在导入 bootstrap sass 文件之前设置字形的图标路径。即

$icon-font-path: '~bootstrap-sass/assets/fonts/bootstrap/';

// Import bootstrap
@import "node_modules/bootstrap-sass/assets/stylesheets/bootstrap";

我在使用 material 设计字体时遇到了同样的问题

src: url("../font/material-design-icons/Material-Design-Icons.eot?#iefix") format("embedded-opentype"), url("../font/material-design-icons/Material-Design-Icons.woff") format("woff"), url("../font/material-design-icons/Material-Design-Icons.ttf") format("truetype"), url("../font/material-design-icons/Material-Design-Icons.svg#Material-Design-Icons") format("svg");

正确的做法是在行首添加一个“~”:

src: url("~/font/material-design-icons/Material-Design-Icons.eot?#iefix") format("embedded-opentype"), url("../font/material-design-icons/Material-Design-Icons.woff") format("woff"), url("../font/material-design-icons/Material-Design-Icons.ttf") format("truetype"), url("../font/material-design-icons/Material-Design-Icons.svg#Material-Design-Icons") format("svg");

把这个放在你的.angular-cli.json:

        "stylePreprocessorOptions": {
            "includePaths": [
                "../node_modules"
            ]
        },