25
loading...
This website collects cookies to deliver better user experience
redirectTo
. This provides more details about a specific route plus a component to load on navigation. Paths are part URLs that are used to locate a resource. ----------------------------
{
path: '',
component: myDashboardComponent,
},
{
path: 'myPath',
component: MyPathComponent
}
------------------------------
RouterModule
and added to the template as shown below:<router-outlet></router-outlet>
import { Injectable } from '@angular/core';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router} from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from '@app/core/services';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate
{
constructor(private router: Router, private authService: AuthService) { }
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree
{
const user = this.authService.user;
if (user)
{
// user authentication successful
return true;
}
// authentication failed, redirect user to login page
this.router.navigate(['/login']);
return false;
}
}
CanActivate
while overriding the canActivate()
method returning a boolean.<a href='#'>link</a>
tags. In an Angular application, href
in the <a>
tag is replaced with the routerLink
as shown below:<a routerLink="'/testLink'">my Angular Link</a> //
<a routerLinkActive="'/testLink'">my Angular Link</a> // for active links
ng new routing-example
Yes/No
questions as shown below:// while creating a new angular project, these sets of questions are displayed.
--------------------------
? Do you want to enforce stricter type checking and stricter bundle budgets in t
he workspace?
This setting helps improve maintainability and catch bugs ahead of time.
For more information, see https://angular.io/strict No
? Would you like to add Angular routing? Yes
? Which stylesheet format would you like to use? (Use arrow keys)
❯ CSS
SCSS [ https://sass-lang.com/documentation/syntax#scss ]
Sass [ https://sass-lang.com/documentation/syntax#the-indented-syntax ]
Less [ http://lesscss.org ]
Stylus [ https://stylus-lang.com
---------------------------
Yes
for the Angular routing option to generate the routing module for our application. cd routing-example
ng g component my-dashboard && ng g component student
app.routing-module.ts
and update the routes as shown below:// app.routing-module.ts has the following contents
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{
path: '',
component: MyDashboardCompoent,
},
{
path: 'students',
component: studentComponent,
},
];
@NgModule({
imports: [
RouterModule.forRoot(routes)
],
exports: [
RouterModule
],
})
export class AppRoutingModule { }
import { Routes, RouterModule } from '@angular/router';
imports the Routes and RouterModule from the router package.RouterModule
and pass it the routes we defined via the RouterModule.forRoot(routes)
method.RouterModule
accessible by other modules by exporting it. app.component.html
as seen below:<h4>My First Single page application</h4>
<router-outlet></router-outlet>
app. routing-module
in the app. module
.-------------------------------------------
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { StudentComponent } from './app.component';
import { MyDashboardComponent } from './app.component';
@NgModule({
declarations: [
AppComponent,
MyDashboardComponent,
StudentComponent
],
imports: [
---------------
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
cd routing-example
ng serve
4200
by default or the immediate port if 4200
is in use. You can now navigate to this route and test your routes.