Angular Routing: Difference between revisions
Jump to navigation
Jump to search
(4 intermediate revisions by the same user not shown) | |||
Line 19: | Line 19: | ||
==Activating the routes== | ==Activating the routes== | ||
This is done via the routeLink for example | |||
<syntaxhighlight lang="html"> | |||
<li class='nav-item'><a class='nav-link' routerLinkActive='active' | |||
[routerLink]="['/welcome']">Home</a> | |||
</li> | |||
</syntaxhighlight> | |||
==Identifying where to place the route templates== | ==Identifying where to place the route templates== | ||
This is via the router-outlet in the component. | |||
<syntaxhighlight lang="ts"> | |||
@Component({ | |||
selector: 'pm-root', | |||
template: ` | |||
... | |||
<div class='container'> | |||
<router-outlet></router-outlet> | |||
</div> | |||
`, | |||
styleUrls: ['./app.component.css'] | |||
}) | |||
export class AppComponent { | |||
pageTitle = 'Acme Product Management'; | |||
} | |||
</syntaxhighlight> | |||
=Creating a Guard= | |||
==Example Guard== | |||
This guard protects the user from navigating away for the form is there has been data entered. | |||
<syntaxhighlight lang="ts"> | |||
import { Injectable } from '@angular/core'; | |||
import { CanDeactivate } from '@angular/router'; | |||
import { Observable } from 'rxjs'; | |||
import { ProductEditComponent } from './product-edit.component'; | |||
@Injectable({ | |||
providedIn: 'root' | |||
}) | |||
export class ProductEditGuard implements CanDeactivate<ProductEditComponent> { | |||
canDeactivate(component: ProductEditComponent): Observable<boolean> | Promise<boolean> | boolean { | |||
if (component.productForm.dirty) { | |||
const productName = component.productForm.get('productName').value || 'New Product'; | |||
return confirm(`Navigate away and lose all changes to ${productName}?`); | |||
} | |||
return true; | |||
} | |||
} | |||
</syntaxhighlight> |
Latest revision as of 13:41, 7 September 2020
Introduction
Setting up Routing requires three steps
- Configuring the routes
- Activating the routes
- Identifying where to place the route templates
Configuring the routes
We define a path, a component and optionally a guards which provides permissions
RouterModule.forChild([
{ path: 'products', component: ProductListComponent },
{ path: 'products/:id', component: ProductDetailComponent },
{
path: 'products/:id/edit',
canDeactivate: [ProductEditGuard],
component: ProductEditComponent
}
])
Activating the routes
This is done via the routeLink for example
<li class='nav-item'><a class='nav-link' routerLinkActive='active'
[routerLink]="['/welcome']">Home</a>
</li>
Identifying where to place the route templates
This is via the router-outlet in the component.
@Component({
selector: 'pm-root',
template: `
...
<div class='container'>
<router-outlet></router-outlet>
</div>
`,
styleUrls: ['./app.component.css']
})
export class AppComponent {
pageTitle = 'Acme Product Management';
}
Creating a Guard
Example Guard
This guard protects the user from navigating away for the form is there has been data entered.
import { Injectable } from '@angular/core';
import { CanDeactivate } from '@angular/router';
import { Observable } from 'rxjs';
import { ProductEditComponent } from './product-edit.component';
@Injectable({
providedIn: 'root'
})
export class ProductEditGuard implements CanDeactivate<ProductEditComponent> {
canDeactivate(component: ProductEditComponent): Observable<boolean> | Promise<boolean> | boolean {
if (component.productForm.dirty) {
const productName = component.productForm.get('productName').value || 'New Product';
return confirm(`Navigate away and lose all changes to ${productName}?`);
}
return true;
}
}