简体   繁体   中英

How to add the class to child component layout from parent component in Angular 12 and Typescript?

I have included the child component in parent component, I have dashboard component as a child component and preview component as a parent component.

Parent(preview) component.html file

<app-dashboard [selectedItems]="menus" [filteredItems]="newuniq"></app-dashboard>

From parent component I am passing some data to child component using @Inpot decorator. What I need is, I have to add some css to that child component layout. I don't know how to do this.

If you want to style your child component's elements (eg. the h1 inside app-dashboard), you can try using ::ng-deep in the scss file of your parent component. However, this is deprecated but it's the easiest to use and it still works.

    ::ng-deep app-dashboard {
    // styles you want to apply app-dashboard's elements
      h1 {
        font-size: 36px;
      }
    }

If you want to style your child component only , you can just do normal scss in your parent scss:

    app-dashboard {
    // styles you want to apply to app-dashboard
    }

The correct way to pass styles is by changing the view encapsulation to none but this will make your styles for the component global. This may mean that other styles will start affecting your component's styles. You can check out the documentation here: View Encapsulation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM