0

Issue details

There is just one route in the code and that is the default route for login component. Due to this line of code <router-outlet></router-outlet> which is present in the app.component.html, my html gets disturbed. there is no problem, if the parent div tag in the login component comes directly inside this div <div class="row justify-content-center">. Is there any way to fix this as there are other component with different classes at their root.

code in app-routing-module

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import {LoginComponent} from './auth/login/login.component';

const routes: Routes = [
    {
        component: LoginComponent,
        path: ''
    }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

code in app.component.html

<header></header>
<div class="container-fluid">
    <div class="row justify-content-center">
        <router-outlet></router-outlet>
    </div>
</div>

Code in Login.component.html

<div class="col-md-4">
    <main class="py-4">
    </main>
</div>

1 Answer 1

1

If you want to apply some class to the root level of LoginComponent you can use the host property (based on inheritance):

@Component({
    selector: "app-login",
    templateUrl: "./login.component.html",
    styleUrls: ["./login.component.css"],
    host: {
        class: "col-md-4",
    },
})
export class LoginComponent {...}

The result output:

<div class="row justify-content-center">
  <router-outlet></router-outlet>
  <app-login class="col-md-4">...</app-login>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

I was looking to set class to the component tag when the route == login, can it be done?
@Pankaj updated the answer to work only with classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.