0

How do I navigate from /login path to /monitor path using this.router.navigate? Similar question but doesn't work for me: How do I navigate to a sibling route? Thanks!


const routes: Routes = [
  {
    path: '', redirectTo: 'monitor', pathMatch: 'full',
  },
  {
    path: 'monitor',
    canActivate: [AuthGuard],
    children: [
      {
        path: '', redirectTo: 'dell', pathMatch: 'full'
      },
      {
        path: 'lenovo', component: lenovoMonitorComponent
      },
      {
        path: 'dell', component: dellMonitorComponent
      },
    ]
  },
  { path: 'login', component: LoginComponent, data: { title: 'Login' }, }
];
Sky
  • 79
  • 1
  • 8

3 Answers3

1

In html you would use routerLink as an relative url, e.g: <a routerLink="/bikes" routerLinkActive="active">Bikes</a> but in typescript you would need to add the url in navigate as a string enclosed by brackets.

@Component({
  selector: 'custom-attribute',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {

  constructor(private router: Router) {
  }

  ngOnInit() {
    this.goToPlaces();
  }

  goToPlaces(){
    setTimeout(() => {
      this.router.navigate(['/bikes'])
    }, 5000)
  }
}

Here is an working example for you: https://stackblitz.com/edit/angular-routing-navigation-example-zivqnx?file=app%2Fapp.component.ts

Joosep Parts
  • 5,372
  • 2
  • 8
  • 33
1

In your case after successful login, you can use the below code to navigate to the /monitor

this.router.navigate(['/monitor']);
Elikill58
  • 4,050
  • 24
  • 23
  • 45
Rishi
  • 11
  • 3
-1

Turns out it is not a routing problem. The answer here How do I navigate to a sibling route? should work.

Sky
  • 79
  • 1
  • 8