2017-06-26 11 views
2

私は再利用可能なコンポーネント "two-column-layout-wrapper"を持っています。スタイルはメインファイル.cssにあります。私は別のモジュールでこのコンポーネントを使いたいですが、私は2つのクラスの異なる幅が必要です。スタイルを上書きするにはどうしたらいいですか?Angular2で再利用可能なコンポーネントのスタイルをオーバーライドする方法は?

import { Component } from '@angular/core'; 
import { FormBuilder, FormGroup, Validators } from '@angular/forms'; 
import { Response } from '@angular/http'; 


@Component({ 
selector: 'italik-managepackages', 
styles: [` 
    :host { 
     display: flex; 
    } 
`], 
template: ` 
    <div two-column-layout-wrapper> 
     <div sidebar-component italik-managepackages-overview></div> 
    </div> 
` 
}) 
export class ManagePackagesComponent { 

} 
+1

、新しいカスタムCSSを作成し、ルールを複製し、新しいプロパティ値にそれがOKメイン1 – LGSon

答えて

2

あなたにはいくつかのオプションがあります。

最初に、親からクラス/スタイルを取得し、内部要素を変更する必要がある場合は、親要素CSSの/deep/セレクタを使用できます。

また、CSSを子コンポーネントに保持する場合は、属性に応じて独自のクラスを設定することで、子がそれに応じて反応するように親から子に属性を渡すことができます渡された)@Inputデコレータを使用します。詳細な説明here

+0

を_after_負荷を設定します。しかし、私はこのスタイルをどこに置く必要がありますか? スタイル:[' :host { display:flex; } .max-width-300px/deep/.max-width-500px { width:800px!important; } ']、 このようにしますか? – Italik

+0

はい、これを親コンポーネントのスタイルに入れると、子コンポーネントに影響します。 –

2

そうですか?

@Component({ 
    template: ` 
    <h1>Parent/h1> 
    <child [style.color]="'blue'"></test-cmp> 
    ` 
}) 
export class ParentComponent { 
} 

@Component({ 
    selector: 'child', 
    template: ` 
    <h1>Child text should be blue</h1> 
    `, 
    styles: [` 
    :host{color: red; } 
    ` 
}) 
export class ChildComponent { 
} 
関連する問題