我正在尝试从主要组件设置 html 和 body 的样式:
app.component.styl:
html, body {
padding: 0;
margin: 0;
background: blue;
}
似乎它不适用于我的 Angular 组件:
app.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.styl']
})
export class AppComponent {
}
app.component.html:
<div class="todo-app">
<div>
<input type="text">
<button>Add</button>
</div>
</div>
为什么 html 和 body 选择器不起作用,是否有特殊的 Angular 方法来实现??
最佳答案
app.component
位于 inside index.html 的 body 标签;像这样
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Angular Alerts</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="assets/img/favicon/favicon.ico">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>
<app-root></app-root> <!-- This is your App Component -->
</body>
</html>
鉴于此,您不能使用默认行为(这意味着如果启用了 ViewEncapsulation)来定位 body
或 html
,因为样式仅限于该组件,并且组件本身没有有body
标签;您需要在 styles.css
全局文件中定义样式,或者为该组件关闭 ViewEncapsulation,以便在其 css 文件中定义的样式全局应用
关于javascript - 如何从主要的 Angular 组件设置 html 和 body 的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53766350/