Angular 2: How to correctly automatically import n

2020-06-30 04:59发布

I am a new Angular 2 user, and I have some problems with it.

Traditionally, we could use <link rel="stylesheet" href="node_modules/normalize.css/normalize.css" /> to import a css file, but I want to make Angular 2 to automatically import it using import.

I tried to use the same way when I used 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';

The editor will complain:

Cannot find module 'normalize-path'.

And the code won't compile. But I really have no idea what was wrong.

4条回答
放荡不羁爱自由
2楼-- · 2020-06-30 05:13

Based on this answer, all needed to do was:

  1. Install the normalize.css library:

    npm install --save normalize.css
    
  2. Import it in your styles.css

    @import '~normalize.css';
    
查看更多
兄弟一词,经得起流年.
3楼-- · 2020-06-30 05:20

Update for Angular 8

  1. Install the normalize.css library:

    npm install --save normalize.css
    
  2. Import it in your styles.css

    @import '~normalize.css';
    

With the current (1.0.0-beta.15) version of angular-cli, the solution is quite easy:

  1. npm i normalize.css
  2. add "../node_modules/normalize.css/normalize.css" in apps[0].styles in the config file angular-cli.json

Note: If using Angular 7, the config file is now angular.json, and the path to normalise.css in apps[0].styles should be "../node_modules/normalize.css/normalize.css".

Example:

{
  "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
  }
}
查看更多
狗以群分
4楼-- · 2020-06-30 05:29

// 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'
    ]
  });
};

and simple add the css link to index.html

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

查看更多
forever°为你锁心
5楼-- · 2020-06-30 05:34

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"
  ],
查看更多
登录 后发表回答