ROUTING : Angular RouterModule there services

yotube
0

 The Angular Router enables navigation from one view to the next as users perform application tasks.

        Router outlet : you've placed in the host view's HTML

        Router links  : 


<base href> : Most routing applications should add a <base> element to the index.html as the first child in the <head> tag to tell the router how to compose navigation URLs.

                <base href="/">


Router imports:


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


Configuration:

configures the router via the RouterModule.forRoot() method.


Router outlet:

The RouterOutlet is a directive from the router library that is used like a component. It acts as a placeholder that marks the spot in the template where the router should display the components for that outlet.


<router-outlet></router-outlet>


Router links:

The RouterLink directives on the anchor tags give the router control over those elements. 

<a routerLink="/list" routerLinkActive="active">Crisis Center</a>


RouterLinkActive:

The RouterLinkActive directive toggles css classes for active RouterLink bindings based on the current RouterState.

<a routerLink="/list" routerLinkActive="active">Crisis Center</a>


Router Service:

router.navigate(['detail', 33]);
router.navigateByUrl("/team/33/user/11");


Router state:

You can access the current RouterState from anywhere in the application using the Router service and the routerState property.



Activated route:

The route path and parameters are available through an injected router service called the ActivatedRoute. 

1 import { ActivatedRoute } from '@angular/router';

2 constructor(private route: ActivatedRoute){}

3A this.route.params.subscribe(params => {
const id: number = +params['id'];
});

3B let id = this.route.snapshot.paramMap.get('id');



Router events:

During each navigation, the Router emits navigation events through the Router.events property.



Example

--src/app/app-routing.module.ts--


import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
const routes: Routes = [
{
path: '',
redirectTo: 'home',
pathMatch: 'full'
},
{
path: 'home',
loadChildren: './pages/home/home.module#HomePageModule'
},
{ path: 'detail/:id', loadChildren: './pages/detail/detail.module#DetailPageModule' },
{ path: '**', component: PageNotFoundComponent }
];

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



--src/app/app.module.ts--


import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
@NgModule({
imports: [
BrowserModule,
FormsModule,
AppRoutingModule
],
declarations: [
AppComponent
],
bootstrap: [ AppComponent ]
})
export class AppModule { }


REFERENCE : https://angular.io/guide/router





Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top