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.
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.
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.
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.
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.
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!!!
try change this:
{
path: 'login',
loadChildren: './login/login.module#LoginModule'
}
to this:
{
path: 'login',
loadChildren: () => LoginModule
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With