gpt4 book ai didi

javascript - 为什么简单的 html 和 css 在 Angular 2 元素中的工作方式与 vanilla html 网站不同

转载 作者:太空宇宙 更新时间:2023-11-04 08:49:03 24 4
gpt4 key购买 nike

在使用“ng new ProjectName”通过 Angular CLI 创建的新 Angular 元素中,以下 html/css 的工作方式不同。我将 CSS 添加到 app.component.css 或 styles.css,将 html 添加到 app.component.html。在 Angular 版本中,它似乎忽略了我 body 的高度。在 sidenav 或内容部分滚动时,标题不会保持固定。

更新:我注意到从 Angular 版本中删除 app-root 标签时一切正常。为什么 app-root 导致它不起作用。

工作 HTML/CSS:

body {
height: 100vh;
margin: 0;
display: flex;
flex-direction: column;
}
#row2 {
display: flex;
flex: 1 0 0;
}
.header {
height: 64px;
background-color: red;
}
.sidenav {
flex: 0 0 250px;
background-color: yellow;
}
.main-content {
flex: 1 0 0;
background-color: blue;
}
.sidenav, .main-content {
overflow-y: scroll;
}
<div id="row1">
<div class="header"></div>
</div>
<div id="row2">
<div class="sidenav"> Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>
</div>
<div class="main-content">
Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>
</div>
</div>

这是 Angular 2 的输出:(请注意,在 css 文件中,我添加的 css 就像在上面的代码片段中一样,Angular 向其添加了 ngcontent 标签,而额外的 flex 样式只是从 chrome 开发者工具添加)

body {
height: 100vh;
margin: 0;
display: flex;
flex-direction: column;
}
#row2[_ngcontent-c0] {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-flex: 1;
-ms-flex: 1 0 0px;
flex: 1 0 0;
}
.header[_ngcontent-c0] {
height: 64px;
}
.sidenav[_ngcontent-c0] {
-webkit-box-flex: 0;
-ms-flex: 0 0 250px;
flex: 0 0 250px;
}
.main-content[_ngcontent-c0] {
-webkit-box-flex: 1;
-ms-flex: 1 0 0px;
flex: 1 0 0;
}
.sidenav[_ngcontent-c0], .main-content[_ngcontent-c0] {
overflow-y: scroll;
}
<html><head>
<meta charset="utf-8">
<title>FlexTest</title>
<base href="/">

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root _nghost-c0="" ng-version="4.0.3">
<div _ngcontent-c0="" id="row1">
<div _ngcontent-c0="" class="header">HEADER</div>
</div>
<div _ngcontent-c0="" id="row2">
<div _ngcontent-c0="" class="sidenav">
Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">
Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">
Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">
</div>
<div _ngcontent-c0="" class="main-content">
Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">
Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">
Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">
</div>
</div>
</app-root>

</body></html>

最佳答案

您不能设置 <body> 的样式来自 CSS 的标签添加到具有默认 View 封装的组件 ViewEncapsulation.emulated .如果您想在 Angular 应用程序之外设置样式,请将 CSS 添加到 index.html .

作为 hack,您可以通过设置禁用 View 封装

@Component({
encapsulation: ViewEncapsulation.none,

关于javascript - 为什么简单的 html 和 css 在 Angular 2 元素中的工作方式与 vanilla html 网站不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43574159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com