2017-06-02 61 views
0

我在角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; 
} 
} 
+0

如果您顯示detail.component.ts的工作方式,它可以很好。 – tildy

+0

@tildy請在以下位置找到detail.component.ts的示例代碼 – Mythri

回答

0

您是否嘗試過使用hostbinding並在其中添加新類?

@HostBinding('class.blueClass') blue: boolean = false; 

而在第二個組件只是切換onInit?

ngOnInit() { 
    this.blue = true; 
} 

的另一種方式可以在組件定義,您可以添加以下行:

host: {'class': 'blueClass'} 

和你的CSS工作休息CSS代替。

+0

我需要添加此@HostBinding('class.someClass')blue:boolean = false ;.在哪個組件中? – Mythri

+1

添加到您的detail.component.ts文件中。從角度/核心導入HostBinding裝飾器 –

+0

在詳細信息組件中。 – tildy

1

在子組件中,您可以將此參數添加到@Component。

兒童component.component.ts

@Component({ selector: 'child-component', templateUrl: 'child-component.component.html', styleUrls: ['child-component.component.css'] encapsulation: ViewEncapsulation.None })

兒童component.component.css

.container-fluid content{background-color: blue;}

您可以參考這一面更infomartion:

https://blog.thoughtram.io/angular/2015/06/29/shadow-dom-strategies-in-angular2.html

+0

我得到錯誤未能加載資源'child-component.component.css' – Mythri

+0

你能給詳細錯誤,文件夾結構,構建工具嗎? –

+0

你創建了它的CSS?在你的結構應該是在這裏: - 細節 -detail.component.html -detail.component.ts -detail.component.css,你應該使用styleUrls:[ 'detail.component.css'] – tildy

相關問題