Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply class to every routed component in Angular

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.

like image 842
Sunil Garg Avatar asked Jul 31 '17 11:07

Sunil Garg


People also ask

How many instance of the router service should a routed Angular application have?

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.


1 Answers

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>

like image 67
Ludevik Avatar answered Sep 24 '22 08:09

Ludevik