Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

router.navigate is not working (Angular6, lazy loading)

I'm a newbie for Angular 4+ (currently using v.6). I've been trying to use this.router.navigate(['/landing']) function to redirect from login component to landing component, it's not working properly. It will show the landing page for a sec then redirect back to login page again.

But if I try to navigate through a normal link, for example,

<a routerLink="/landing">landing</a>

It work properly.

Here is my app-routing.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  {
    path: 'login',
    loadChildren: './login/login.module#LoginModule'
  },
  {
    path: 'landing',
    loadChildren: './landing/landing.module#LandingModule'
  },
  {
    path: '',
    redirectTo: 'landing',
    pathMatch: 'full'
  },
  {
    path: '**',
    redirectTo: 'landing',
    pathMatch: 'full'
  }
];

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

And here is the code in the login component,

export class LoginComponent implements OnInit {

  constructor(private router: Router) { }

  ngOnInit() {
  }

  login(){
    this.router.navigate(['/landing']);
  }
  
}

Here is the code in landing-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { LandingComponent } from './landing.component';

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

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

Thanks for helps.

like image 919
Hikaru Shindo Avatar asked May 18 '18 15:05

Hikaru Shindo


People also ask

What is routing and lazy loading?

Lazy loading is a technique in Angular that allows you to load JavaScript components asynchronously when a specific route is activated. It improves the speed of the application load time by splitting the application into several bundles. When the user navigates through the app, the bundles are loaded as required.

What is lazy loading feature?

Lazy loading is the process of loading components, modules, or other assets of a website as they're required. Since Angular creates a SPA (Single Page Application), all of its components are loaded at once. This means that a lot of unnecessary libraries or modules might be loaded as well.

Can I use routerLink on button?

If you need logic to occur before you go to a route you can import the Router Module and use it as such. I'm unsure of the best practice but I would say that it is fine to use routerLink and (click) in the same button as long as you do not interfere with the navigation. Manually navigation via this. router.

What does Router navigate do in Angular?

In Angular, RouterLink is a directive for navigating to a different route declaratively. Router. navigate and Router. navigateByURL are two methods available to the Router class to navigate imperatively in your component classes.


2 Answers

I faced similar issue, how I resolved it:

In Component.ts

constructor(private router: Router, private route: ActivatedRoute){}

this.router.navigate(['../landing'], {relativeTo: this.route})

try this and let me know if it helps!!!

like image 81
BHAVYA BHUSHAN Avatar answered Oct 01 '22 17:10

BHAVYA BHUSHAN


try change this:

{
  path: 'login',
  loadChildren: './login/login.module#LoginModule'
}

to this:

{
  path: 'login',
  loadChildren: () => LoginModule
}
like image 25
Andro Sid Avatar answered Oct 01 '22 16:10

Andro Sid