Is there any way to apply a class to every routed component in Angular. One way is to use host
property for each component like
@Component({
moduleId: module.id,
selector: 'one',
host :{class :'my-class'}
templateUrl: 'one.html',
})
but I don't want to write this for every component.
A routed Angular application has one singleton instance of the Router service. When the browser's URL changes, that router looks for a corresponding Route from which it can determine the component to display.
Do you really need to add class or just style all routed components? You can add this to style all routed components:
router-outlet + * {
//your styles
}
For angular 6+ use:
router-outlet + ::ng-deep * {
//your styles
}
Explanation:
Angular inserts the components next to <router-outlet>
tag, so the rendered html looks like:
<router-outlet></router-outlet>
<some-component></some-component>
router-outlet + *
is css selector for any next sibling of <router-outlet>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With