typescript Angular 2:如何正确自动导入 normalize.css
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38409773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular 2: How to correctly automatically import normalize.css
提问by Garfield550
I am a new Angular 2 user, and I have some problems with it.
我是一个新的 Angular 2 用户,我遇到了一些问题。
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
.
传统上,我们可以<link rel="stylesheet" href="node_modules/normalize.css/normalize.css" />
用来导入一个 css 文件,但我想让 Angular 2 使用import
.
I tried to use the same way when I used Material 2:
当我使用材料 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.
并且代码不会编译。但我真的不知道出了什么问题。
回答by Nicolas RTT
Update for Angular 8
Angular 8 更新
Install the normalize.css library:
npm install --save normalize.css
Import it in your styles.css
@import '~normalize.css';
安装 normalize.css 库:
npm install --save normalize.css
将它导入你的styles.css
@import '~normalize.css';
With the current (1.0.0-beta.15
) version of angular-cli, the solution is quite easy:
使用当前 ( 1.0.0-beta.15
) 版本的 angular-cli,解决方案非常简单:
npm i normalize.css
- add
"../node_modules/normalize.css/normalize.css"
inapps[0].styles
in the config fileangular-cli.json
npm i normalize.css
- 添加
"../node_modules/normalize.css/normalize.css"
在apps[0].styles
在配置文件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"
.
注意:如果使用 Angular 7,配置文件现在是angular.json
,并且 normalise.css 的路径apps[0].styles
应该是"../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
}
}
回答by elpddev
Based on this answer, all needed to do was:
基于这个答案,所有需要做的是:
Install the normalize.css library:
npm install --save normalize.css
Import it in your styles.css
@import '~normalize.css';
安装 normalize.css 库:
npm install --save normalize.css
将它导入你的styles.css
@import '~normalize.css';
回答by Stefanie Fluin
The accepted response doesn't seem to be working on app. I needed to remove the ../
in the path name.
接受的响应似乎不适用于应用程序。我需要删除../
路径名中的 。
The angular.jsonstyles bit should be something like this:
该angular.json款式位应该是这样的:
"styles": [
"node_modules/normalize.css/normalize.css",
"styles.css"
],
回答by Kiss Robert
// 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
并简单地将 css 链接添加到 index.html
// index.html
<link href="vendor/normalize.css/normalize.css" rel="stylesheet">