javascript - 如何从主要的 Angular 组件设置 html 和 body 的样式?

标签 javascript angular

我正在尝试从主要组件设置 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)来定位 bodyhtml,因为样式仅限于该组件,并且组件本身没有body标签;您需要在 styles.css 全局文件中定义样式,或者为该组件关闭 ViewEncapsulation,以便在其 css 文件中定义的样式全局应用

关于javascript - 如何从主要的 Angular 组件设置 html 和 body 的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53766350/

相关文章:

javascript - 减少嵌套的 Promise

html - 如何在 Angular 中的整个页面上添加透明背景图像?

带有@angular/pwa 包的 Angular Service Worker 离线时显示 `HTTP error 504`

javascript - 无法让 "disallowSpacesInsideParentheses"工作

javascript - puppeteer 师 : Timeout or sleep or wait inside Page. 评估()

javascript - socket.on 内部未定义套接字

html - 在 Flex 行之间添加空间

angularjs - [FromBody] 中的字节数组和 JSON

Angular Material - 设置调色板

javascript - 钛手机: App crashes after loading first window