30

我是一个新的 Angular 2 用户,我遇到了一些问题。

传统上,我们可以使用<link rel="stylesheet" href="node_modules/normalize.css/normalize.css" />导入 css 文件,但我想让 Angular 2 使用import.

我在使用 Material 2 时尝试使用相同的方式:

// angular-cli-build.js

module.exports = function(defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
      'normalize-path/index.js',
    ]
  });
};

// system-config.ts 

const map: any = {
  'normalize': 'vendor/normalize-path',
};

/** User packages configuration. */
const packages: any = {
  'normalize': {main: 'index.js'},
};

// app.component.ts

import { normalize } from 'normalize-path';

编辑会抱怨:

找不到模块“规范化路径”。

并且代码不会编译。但我真的不知道出了什么问题。

4

4 回答 4

57

Angular 8 的更新

  1. 安装 normalize.css 库:

    npm install --save normalize.css
    
  2. 将其导入您的 styles.css

    @import '~normalize.css';
    

使用当前 ( 1.0.0-beta.15) 版本的 angular-cli,解决方案非常简单:

  1. npm i normalize.css
  2. "../node_modules/normalize.css/normalize.css"apps[0].styles配置文件中添加angular-cli.json

注意:如果使用 Angular 7,配置文件现在是angular.json,而 normalise.css 的路径apps[0].styles应该是"../node_modules/normalize.css/normalize.css".

例子:

{
  "project": {
    "version": "1.0.0-beta.15",
    "name": "normalize.css-in-angular2"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": "assets",
      "index": "index.html",
      "main": "main.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "mobile": false,
      "styles": [
        "../node_modules/normalize.css/normalize.css",
        "styles.css"
      ],
      "scripts": [],
      "environments": {
        "source": "environments/environment.ts",
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "addons": [],
  "packages": [],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "prefixInterfaces": false
  }
}
于 2016-09-23T20:51:46.950 回答
38

基于这个答案,所有需要做的是:

  1. 安装 normalize.css 库:

    npm install --save normalize.css
    
  2. 将其导入您的 styles.css

    @import '~normalize.css';
    
于 2017-08-03T12:43:49.227 回答
5

The accepted response doesn't seem to be working on app. I needed to remove the ../ in the path name.

The angular.json styles bit should be something like this:

"styles": [
    "node_modules/normalize.css/normalize.css",
    "styles.css"
  ],
于 2018-07-29T01:05:12.300 回答
0

// angular-cli-build.js

module.exports = function(defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
      'systemjs/dist/system-polyfills.js',
      'systemjs/dist/system.src.js',
      'zone.js/dist/**/*.+(js|js.map)',
      'es6-shim/es6-shim.js',
      'reflect-metadata/**/*.+(ts|js|js.map)',
      'rxjs/**/*.+(js|js.map)',
      '@angular/**/*.+(js|js.map)',
      '@angular2-material/**/*.+(js|js.map)',
      'normalize.css/normalize.css'
    ]
  });
};

并简单地将css链接添加到index.html

// index.html
<link href="vendor/normalize.css/normalize.css" rel="stylesheet">

于 2016-07-17T09:10:27.093 回答