angular-cli包如何css?

时间:2017-07-18 15:00:25

标签: css webpack angular-cli

我正在努力使用angular-cliwebpack将Angular 2项目转换为Angular 4。我遇到了从scss文件访问图片资源的问题。我认为我的问题是由于scss文件的目录结构被展平的方式引起的。在我的项目的src目录中,我有一个stlyes子目录和一个assets子目录(它是styles目录的兄弟)。两个目录都有嵌套的子目录。我目前认为打包时assets目录保持其子目录结构并且styles目录被展平,因此导致对展平css的资产的相对引用不正确。

例如/src/styles/partials/_sprite.scss

.icon {
  background: url("../../assets/images/sprite-svg-ui.svg") no-repeat;
}

在我的IDE中,此引用正确解析,但是当angular-cli构建时,我收到以下错误:

ERROR in ./~/css-loader?{"sourceMap":false,"importLoaders":1}!./~/postcss-loader?{"ident":"postcss"}!./~/sass-loader/lib/loader.js?{"sourceMap":false,"precision":8,"includePaths":[]}!./src/styles/index.scss
Module not found: Error: Can't resolve '../../assets/images/sprite-svg-ui.svg' in 'C:\Users\jhart\Desktop\pascal.ui\src\styles'
 @ ./~/css-loader?{"sourceMap":false,"importLoaders":1}!./~/postcss-loader?{"ident":"postcss"}!./~/sass-loader/lib/loader.js?{"sourceMap":false,"precision":8,"includePaths":[]}!./src/styles/index.scss 6:43140-43188
 @ ./src/styles/index.scss
 @ multi ./src/styles/index.scss

这是我的.angular-cli.json文件:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "ui"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets"
      ],
      "index": "index.html",
      "main": "index.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "./styles/index.scss"
      ],
      "scripts": [
        "dot.dot.dot.js"
      ],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "project": "src/tsconfig.app.json"
    },
    {
      "project": "src/tsconfig.spec.json"
    },
    {
      "project": "e2e/tsconfig.e2e.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "scss",
    "component": {},
    "serve": {
      "port": 3000
    }
  }
}

以下是我在index.scss文件的apps\styles元素中引用的.angular-cli.json文件:

// globals

@import 'functions';
@import 'variables';
@import 'mixins';
@import 'base';
@import 'boxer';

// partials

@import 'partials/grid';
@import 'partials/sprite';
@import 'partials/typography';
@import 'partials/buttons';
@import 'partials/forms';
@import 'partials/tables';
@import 'partials/utility';

// vendors
// @import 'vendors/chosen';

@import 'vendors/chartist/chartist';
@import 'vendors/ng2-select';

// component (declarations)

@import 'partials/wrapper';
@import 'partials/header';
@import 'partials/navigation-primary';
@import 'partials/footer';
@import 'partials/upload-meter';
@import 'partials/pagination';
@import 'partials/breadcrumb';
@import 'partials/tabs';
@import 'partials/cards';
@import 'partials/badges';
@import 'partials/flyouts';
@import 'partials/comments';
@import 'partials/tutorial';
@import 'partials/year-selector';
@import 'partials/upload-window';
@import 'partials/panels';
@import 'partials/charts';
@import 'partials/lists';
@import 'partials/collapse';
@import 'partials/tooltips';
@import 'partials/login';
@import 'partials/settings';

@import '../app/modules/core/components/notification-banner/styles';

// component (declarations)

@import 'partials/cbcr-view';

// directives

@import '../app/modules/core/directives/menu/menu';

更新1

我发现通过绝对路径/app/assets/images/引用图像资源允许构建来解析图像文件,但现在我IDE中对图像文件的引用无效......

使用绝对路径/app/assets/images/运行应用程序构建时,我在加载图像文件时得到404 ...

更新2

我将prefix中的.angular-cli.json参数更改为"",然后从我的/src/styles/partials/_sprite.scss文件中使用绝对路径/assets/images/引用我的图片资源,例如:

.icon-close {
  background: url("/assets/images/close.svg") no-repeat;
  width: 24px;
  height: 24px;
}

我的IDE仍然无法解析图像文件......但至少它会构建并执行。

1 个答案:

答案 0 :(得分:2)

我遇到了这个问题,我通过在你的情况下根据“index.scss”文件的位置放置图像的绝对路径来解决它,而不是依赖于导入的子sass文件“_sprite.scss”。这将从IDE中给出错误引用错误,但它将起作用,您将不会从webpack收到错误。我希望它有所帮助。