Routes at Angular 2+

Asked

Viewed 296 times

0

My application starts with the Logincomponent component:

bootstrap: [LoginComponent]

In my login routing module I set the following routes:

const loginRoutes: Routes = [
  {path: 'dash', component: DashboardComponent},
  {path: 'registrar', component: RegistrarComponent},
];

@NgModule({
  imports:[RouterModule.forRoot(loginRoutes)],
  exports: [RouterModule]
})

Dash must be accessed when the user logs in;

In my login.component.html I set an ngIf that will only appear to Dash when the user logs into the system:

<div *ngIf="!usuarioautenticou" class="wrapper">

In this template I set the router-outlet and buttons for browser calls:

<div>
     <button type="submit" routerLink="/dash" name="entrar" (click)="fazerLogin()" class="btn btn-success waves-effect" id="login-button">Entrar</button>
     <button type="button" routerLink="/registrar" class="btn btn-outline-success waves-effect">Registrar</button>
</div>
<router-outlet></router-outlet>

Everything works perfectly for now. When the application is on localhost:4200 it redirects to the login page. When I click register it changes to the registry component and when I click enter it moves me to the Dash screen.

The problem is that Dash is a fixed component that inside it will have other components, so I tried to put another router-outlet inside Dash:

   <router-outlet></router-outlet>

Dashboard.routing.module.ts:

const dashboardRoutes: Routes = [
  { path: '', component: BemvindoComponent },
];

However, the Bemvindocomponent does not appear inside my Dash. It is already as Exports and imported by my Dash route module.

1 answer

1

You have to put a name to the second router-outlet

<router-outlet name="nomeASerDado"></router-outlet>

When defining on routes, specify the desired outlet

{path: 'caminho/a/ser/seguido', component: ComponenteQueSeraCarregado, outlet: 'nomeASerDado'}

And to make the route changes, it has two forms:

  1. In HTML, via routerLink

    <a [routerLink]="[{outlets: {primary: ['caminho/da/view/primaria'], nomeASerDado: ['caminho/da/view/auxiliar']}}]"></a>
    
  2. Via a command to the router

    router.navigate([{outlets: {primary: 'caminho/da/view/primaria', nomeASerDado: 'caminho/da/view/auxiliar'}}]);
    

But you only need to use this route change form if you need to use the named outlet, otherwise you can use it like this:

routerLink="caminho/a/ser/seguido"

or so

router.navigate(['caminho/a/ser/seguido'])

You can get some more help of this link.

  • I changed to: {path: 'Dash', Component: Dashboardcomponent, outlet:'primario'}, {path: 'register', Component: Registrarcomponent, outlet:'primario'} and <button type="Submit" routerLink="[{outlets: {primario: ['Dash']" name="enter" (click)="do"btn btn-Success Waves-Effect" id="login-button">Login</button> <button type="button" routerLink="[{outlets: {primary: ['register']" class="btn btn-Outline-Success Waves-Effect">Register</button>

  • You have to put the name attribute in the router-outlet and register the route in the routing module

  • also put... <router-outlet name="primary"></router-outlet>

  • The routerLink on the button is not between square brackets. It should be [routerLink]=

  • using brackets the page does not load... and without brackets does not work

  • On buttons, the attribute value routerLink is not closed properly. You have not closed the outlets nor the [{

  • I fixed it. now the page loads, but my Inbox still does not load inside the second router-outlet. The first I set to name "primary", the second left only router-outlet. I need to name this too?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.