Angular2:如何正确自动导入normalize.css

Angular 2: How to correctly automatically import normalize.css

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

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

我试过用的时候也是一样的方法 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';

小编会吐槽:

Cannot find module 'normalize-path'.

并且代码无法编译。但是我真的不知道哪里出了问题。

// 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 link 添加到 index.html

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

更新 Angular 8

  1. 安装 normalize.css 库:

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

    @import '~normalize.css';
    

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

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

注意:如果使用Angular7,配置文件现在是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
  }
}

基于,所有需要做的是:

  1. 安装 normalize.css 库:

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

    @import '~normalize.css';
    

已接受的回复似乎不适用于应用程序。我需要删除路径名中的 ../

angular.json 样式位应该是这样的:

"styles": [
    "node_modules/normalize.css/normalize.css",
    "styles.css"
  ],