Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Der neue Angular Router im Detail betrachtet
Search
Manfred Steyer
PRO
March 21, 2017
Programming
2
220
Der neue Angular Router im Detail betrachtet
Manfred Steyer
PRO
March 21, 2017
Tweet
Share
More Decks by Manfred Steyer
See All by Manfred Steyer
The Missing Link in Angular‘s Signal Story Resource API and httpResource @ngRome 2025
manfredsteyer
PRO
0
44
Your Architecture as a Crime Scene:Forensic Analysis
manfredsteyer
PRO
0
120
Rethinking Data Access: The New httpResource in Angular
manfredsteyer
PRO
0
280
Reactive Thinking with Signals, Resource API, and httpResource @Devm.io Angular 20 Launch Party
manfredsteyer
PRO
0
180
JavaScript as a Crime SceneForensic Analysis
manfredsteyer
PRO
0
77
Modern Angular with Signals and Signal Store:New Rules for Your Architecture @jax2025 in Mainz, Germany
manfredsteyer
PRO
0
160
Premier Disciplin for Micro Frontends Multi Version/ Framework Scenarios
manfredsteyer
PRO
0
94
Your Architecture as a Crime SceneForensic Analysis
manfredsteyer
PRO
0
68
Live Coding: Migrating an Application to Signals
manfredsteyer
PRO
0
180
Other Decks in Programming
See All in Programming
Webからモバイルへ Vue.js × Capacitor 活用事例
naokihaba
0
690
イベントストーミングから始めるドメイン駆動設計
jgeem
4
850
Perlで痩せる
yuukis
1
680
ktr0731/go-mcpでMCPサーバー作ってみた
takak2166
0
170
AWS CDKの推しポイント 〜CloudFormationと比較してみた〜
akihisaikeda
3
280
C++20 射影変換
faithandbrave
0
480
セキュリティマネジャー廃止とクラウドネイティブ型サンドボックス活用
kazumura
1
170
2度もゼロから書き直して、やっとブラウザでぬるぬる動くAIに辿り着いた話
tomoino
0
160
Cline指示通りに動かない? AI小説エージェントで学ぶ指示書の書き方と自動アップデートの仕組み
kamomeashizawa
1
520
機械学習って何? 5分で解説頑張ってみる
kuroneko2828
0
210
LINEヤフー データグループ紹介
lycorp_recruit_jp
0
670
CSC307 Lecture 17
javiergs
PRO
0
120
Featured
See All Featured
How STYLIGHT went responsive
nonsquared
100
5.6k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
130
19k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
How GitHub (no longer) Works
holman
314
140k
Agile that works and the tools we love
rasmusluckow
329
21k
KATA
mclloyd
29
14k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
228
22k
Speed Design
sergeychernyshev
31
990
Mobile First: as difficult as doing things right
swwweet
223
9.7k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.1k
Transcript
Der neue Angular Router im Detail betrachtet Manfred Steyer SOFTWAREarchitekt.at
ManfredSteyer ManfredSteyer
Über mich … • Manfred Steyer • SOFTWAREarchitekt.at • Trainer
& Consultant • Focus: Angular • Google Developer Expert (GDE) Page ▪ 2 Manfred Steyer
Ziel • Den Router kennen lernen • Erweiterte Möglichkeiten kennen
lernen
Inhalt • Basics zu Routing • Child-Routes • Aux-Routes •
Guards • Lazy Loading • Preloading Page ▪ 4
Motivation Der Router ist der Dreh- und Angelpunkt vieler SPA
Routing Basics Page ▪ 6
Routing unter Angular Page ▪ 7 Logo + Menü Menü
2 Fußzeile SPA Platzhalter
Routing unter Angular Page ▪ 8 Logo + Menü Menü
2 Fußzeile SPA Passagier- Komponente /FlugDemo#passagier
Routen via Hash-Fragment • /FlugDemo#passagier • Hash-Fragment wird nie zum
Server gesendet Page ▪ 9
Routen via History API • /FlugDemo/passagier • Gesamte Url wird
zum Server gesendet • Server antwortet mit SPA • Server kann erste Ansicht "vorrendern" • Performance, SEO, … • SPA informiert Browser über Grenze zwischen (eigentlicher) Url und Route Page ▪ 10
Parameter • passagier/7 • passagier/7?details=true&page=7 • passagier/7;details=true;page=7 • passagier/7;details=true;page=7/fluege Page
▪ 11
Konfiguration Page ▪ 12 const APP_ROUTES: Routes = [ {
path: 'home', component: HomeComponent }, { path: 'flug-suchen', component: FlugSuchenComponent } ]
Konfiguration Page ▪ 13 const APP_ROUTES: Routes = [ {
path: '', redirectTo: 'home', pathMatch: 'full' }, { path: 'home', component: HomeComponent }, { path: 'flug-suchen', component: FlugSuchenComponent } ]
Konfiguration Page ▪ 14 const APP_ROUTES: Routes = [ {
path: '', redirectTo: 'home', pathMatch: 'full' }, { path: 'home', component: HomeComponent }, […] { path: '**', redirectTo: 'home' } ]
Konfiguration Page ▪ 15 export const AppRoutesModule = RouterModule.forRoot(ROUTE_CONFIG); //
app.module.ts @NgModule({ imports: [ BrowserModule, HttpModule, FormsModule, AppRoutesModule ], […] }) export class AppModule { } Für Root-Module Für Feature-Module: forChild
AppComponent Page ▪ 16 @Component({ selector: 'app', templateUrl: './app.html' })
export class App { […] }
View von AppComponent Page ▪ 18 <a routerLink="/">Home</a> <a routerLink="/flug-suchen">Flug
suchen</a> <div> <router-outlet></router-outlet> </div>
Routen mit Parameter Page ▪ 20
Konfiguration Page ▪ 21 const APP_ROUTES: Routes = [ […]
{ path: 'flug-suchen', component: FlugSuchenComponent }, { path: 'flug-edit/:id', component: FlugEditComponent } }
Parameter auslesen Page ▪ 22 export class FlugEditComponent { public
id: string; constructor( private route: ActivatedRoute) { route.params.subscribe( p => { this.id = p['id']; […] } ); } […] }
Links auf Routen mit Parameter Page ▪ 23 <a [routerLink]="['/flug-edit',
flug.id]">Edit</a>
DEMO Page ▪ 24 App Home Search Flight Edit Flight
Search Passenger Your Bookings FlightBookingModule AppModule
Hierarchisches Routing Page ▪ 25
Hierarchische Views Page ▪ 26 Logo + Menü Menü 2
Fußzeile SPA Platzhalter 1
Hierarchische Views Page ▪ 27 Logo + Menü Menü 2
Fußzeile SPA /FlugDemo/flugbuchen FlugBuchen-Komponente
Hierarchische Views Page ▪ 28 Logo + Menü Menü 2
Fußzeile SPA /FlugDemo/flugbuchen Optionen Platzhalter FlugBuchen-Komponente
Hierarchische Views Page ▪ 29 Logo + Menü Menü 2
Fußzeile SPA Optionen Passagier- Komponente FlugBuchen-Komponente /FlugDemo/flugbuchen/passagier
Konfiguration Page ▪ 30 const APP_ROUTES: Routes = [ {
path: '', component: HomeComponent }, { path: 'flug-buchen', component: FlugBuchenComponent, children: [ { path: 'flug-suchen', component: FlugSuchenComponent }, […] ] } ];
DEMO Page ▪ 31 App Home Flight- Booking Search Flight
Edit Flight Search Passenger Your Bookings
Aux-Routes Page ▪ 34
Aux-Routes Page ▪ 35 Logo + Menu Menu 2 Footer
SPA Placeholder Named Placeholder
Aux-Routes Page ▪ 36 Logo + Menu Menu 2 Footer
SPA Flight- Component Named Placeholder /FlightApp/flights
Aux-Routes Page ▪ 37 Logo + Menu Menu 2 Footer
SPA Flight- Component Info-Component /FlightApp/flights(aux:info)
Aux-Routes Page ▪ 38 Logo + Menu Menu 2 Footer
SPA Flight- Component Modal-Component /FlightApp/flights(aux:info/modal)
Aux-Routes Page ▪ 39 Logo + Menu Menu 2 Footer
SPA Flight-Edit- Component Modal-Component /FlightApp/flights(aux:info/modal)/17
Use Cases • Modale Dialoge • “Applets” und teilautonome Bereiche
• Commander-Style Page ▪ 40
Platzhalter definieren Page ▪ 41 <router-outlet></router-outlet> <hr> <router-outlet name="aux"></router-outlet> Standard-Name:
primary
Konfiguration Page ▪ 42 export const ROUTE_CONFIG: Routes = [
{ path: 'home', component: HomeComponent }, { path: 'info', component: InfoComponent, outlet: 'aux' }, { path: 'dashboard', component: DashboardComponent, outlet: 'aux' } ]
Aux-Routes routen Page ▪ 43 <a [routerLink]="[{outlets: { aux: 'info'
}}]"> Activate Info </a> <a [routerLink]="[{outlets: { aux: null }}]"> Deactivate Info </a>
Programmatisch routen Page ▪ 44 export class AppComponent { constructor(private
router: Router) { } activateInfo() { this.router.navigate([{outlets: { aux: 'info' }}]); } deactivateInfo() { this.router.navigate([{outlets: { aux: null }}]); } }
DEMO Page ▪ 45
Guards Page ▪ 46
Was sind Guards? • Services • Werden beim Aktivieren bzw.
Deaktivieren einer Route aktiv • Können Aktivierung und Deaktivierung verhindern Page ▪ 47
Guards Rückgabewert: boolean | Observable<boolean> | Promise<boolean> CanActivate canActivate CanActivateChild
canActivateChild CanLoad canLoad CanDeactivate<T> canDeactivate
Guards in der Konfiguration Page ▪ 50 const APP_ROUTES: Routes
= [ { path: '/flug-buchen', component: FlugBuchenComponent, canActivate: [AuthGuard], children: [ { path: 'flug-edit/:id', component: FlugEditComponent, canDeactivate: [FlugEditGuard] }, […] ] ] Token
Provider für Guards Page ▪ 51 export const ROUTER_PROVIDERS =
[ { provide: FlugEditGuard, useClass: AdvFlightEditGuard } { provide: AuthGuard, useClass: AdvAuthGuard } ];
Provider für Guards Page ▪ 52 export const ROUTER_PROVIDERS =
[ FlugEditGuard, AuthGuard ]; // app.module.ts @NgModule({ providers: [ ROUTER_PROVIDERS ], […] }) export class AppModule { }
DEMO Page ▪ 53
Lazy Loading Page ▪ 61
Warum Lazy Loading? • Module erst bei Bedarf nachladen •
Verbesserung der Start-Performance Page ▪ 62
Root Module mit Lazy Loading Page ▪ 63 const APP_ROUTE_CONFIG:
Routes = [ { path: '', redirectTo: 'home', pathMatch: 'full' }, { path: 'home', component: HomeComponent }, { path: 'flights', loadChildren: '[…]flight-booking.module#FlightBookingModule' } ];
Routen für Feature Module Page ▪ 64 const FLUG_ROUTES =
[ { path: '', component: FlugBuchenComponent, […] }, […] } export const FlugRouterModule = RouterModule.forChild(FLUG_ROUTES);
Konfiguration von Webpack • angular2-router-loader Page ▪ 65
DEMO Page ▪ 66
Preloading Page ▪ 67
Idee • Eventuell später benötigte Module werden mit freien Ressourcen
vorgeladen • Wird das Modul später tatsächlich benötigt, steht es augenblicklich zur Verfügung Page ▪ 68
Preloading aktivieren Page ▪ 69 export const AppRoutesModule = RouterModule.forRoot(
ROUTE_CONFIG, { preloadingStrategy: PreloadAllModules });
Lazy Loading und Shared Services Page ▪ 70
Lazy Loading und Shared Modules Page ▪ 71 AppModule FlugModule
SharedModule includes includes (lazy) includes
Lazy Loading und Shared Modules Page ▪ 72 AppModule FlugModule
SharedModule includes includes (lazy) includes AuthService AuthService
Lazy Loading und Shared Modules Page ▪ 73 AppModule FlugModule
SharedModule includes includes (lazy) includes AuthService AuthService
Lösung Page ▪ 74 AppModule FlugModule SharedModule includes (lazy) includes
CoreModule includes includes Core-Module soll nur ins AppModule eingebunden werden Globale Provider, wie AuthService & Shell
DEMO
Lösung (für Libraries) Page ▪ 76 AppModule FlugModule AuthModule includes
(lazy) CoreModule
Lösung (für Libraries) Page ▪ 77 AppModule FlugModule AuthModule includes
(lazy) includes (mit Services) CoreModule includes includes (ohne Services) Shell
AuthModule Page ▪ 79 @NgModule({ […], providers: [] }) export
class AuthModule { }
AuthModule Page ▪ 80 @NgModule({ […], providers: [] }) export
class AuthModule { static forRoot(): ModuleWithProviders { return { ngModule: AuthModule, providers: [AuthService, […]] } } }
DEMO Page ▪ 81
Zusammenfassung Platzhalter Child Routes Aux Routes Guards Lazy Loading Preloading
Kontakt [mail]
[email protected]
[blog] SOFTWAREarchitekt.at [twitter] ManfredSteyer