Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
752 views
in Technique[技术] by (71.8m points)

sass - Angular 4+ overwrite child component's style

I want to force parent comp to change child comp's style without the !important modifier. My question is.. is there any way to do that? (I am beginner to angular may not the best solution.)

Here is a minimal example about the 2 comps. Child has a simple div in template :)

parent.component.scss:

::ng-deep .child-div{
    background-color: red;
}

childe.component.scss:

.child-div{
    width: 100rem;
    height: 100rem;
    background-color: $pink;
    color: $blue;
    line-height: 100rem;
    text-align: center;
    margin-left: auto;
    margin-right: auto;
}

Thanks for your time and answers! :)

Edit #1 --- I tried it from the global style.scss

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add to your global stylesheet

app-root app-child .child-div {
   background-color: red;
}

You also have to turn off the viewencapsulation of the child component. I've created a stackblitz for demonstration.

Keep in mind in this case you will always override the childs style because the app-component will always be a parent of any child component.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...