我在角2應用程序中有3個組件。class="container-fluid content"
是app.component.html中的css類。這class="container-fluid content"
是其他組件的默認CSS。現在我想在詳細組件中設置background-color:blue
。我試圖設置detail.component.ts像這樣styles:['.container-fluid content{background-color: blue;}']
它沒有工作。如果我像這樣設置了app.component.html <div class="container-fluid content" style="background-color: blue;">
它適用於這兩個組件。如何重寫這個類=「容器流體內容」的詳細組件?如何在其子組件中使用根組件的CSS類 - Angular 2?
//my project structure
app
- app.component.html
-app.component.ts
- welcome
-welcome.component.html
-welcome.component.ts
- detail
-detail.component.html
-detail.component.ts
//app.component.html
<div class="container-fluid content">
<router-outlet></router-outlet>
</div>
<app-footer></app-footer>
</div>
//welcome.component.html
<h1>welcome page heading</h1>
<div fxLayout="row" fxLayoutWrap style="padding-bottom: 25px;
padding-top: 25px; margin: auto;justify-content: center" >
<md-card>
<md-card-content>
<h1></h1>
<h2></h2>
<h2>
</h2>
</md-card-content>
</md-card>
</div>
//detail.component.html
<h1>Details page heading</h1>
<div fxLayout="row" fxLayoutWrap style="padding-bottom: 25px;
padding-top: 25px; margin: auto;justify-content: center" >
<md-card>
<md-card-content>
<h1></h1>
<h2></h2>
<h2>
</h2>
</md-card-content>
</md-card>
</div>
//detail.component.ts
import { OnInit, Component } from '@angular/core';
import { DetailService } from '../detail/detail.service';
import { HostBinding} from '@angular/core';
@Component({
providers: [DetailService ]
templateUrl: './detail.component.html',
styles: ['h3 {margin:5px}']
})
export class DetailComponent implements OnInit {
@HostBinding('class.blueClass') blue: boolean = false;
constructor(private _detailService: DetailService) { }
ngOnInit() {
this.blue = true;
}
}
如果您顯示detail.component.ts的工作方式,它可以很好。 – tildy
@tildy請在以下位置找到detail.component.ts的示例代碼 – Mythri