v3.0.0-alpha.7
https://www.monsite.com/weather/34000?type=awesome#traffic=disabled
Protocol
Domain
Path
Query string
Hash / Fragment
Décrit l'état courant de l'application
Paramètres additionnels
Server
Client
[
{path: 'team/:id', component: TeamCmp, children: [
{path: 'details', component: DetailsCmp},
{path: 'help', component: HelpCmp, outlet: 'aux'}
]},
{path: 'summary', component: SummaryCmp},
{path: 'chat', component: ChatCmp, outlet: 'aux'}
]
Outlets
Summary component
// URL
/team/3/details
// URL Tree
new UrlSegment(paths: [], children: {
primary:
new UrlSegment(paths: [
new UrlPathWithParams(path: 'team', parameters: {}),
new UrlPathWithParams(path: '3', parameters: {}),
new UrlPathWithParams(path: 'details', parameters: {})
], children: {}
)
}
// URL
/team/3(aux:/chat;open=true)
// URL Tree
new UrlSegment(paths: [], children: {
primary:
new UrlSegment(paths: [
new UrlPathWithParams(path: 'team', parameters: {})
new UrlPathWithParams(path: '3', parameters: {})
], children: {}
),
aux:
new UrlSegment(paths: [
new UrlPathWithParams(path: 'chat', parameters: {open: 'true'})
], children: {}
)
}
=> enfant multiple
=> pour spécifier l'outlet
=> Paramètre spécifique à une route
Exemples :
/team/3(aux:/chat;open=true)
/team/3(aux:/help;lang=fr)
/team/3/detail
bootstrap(MyComponent, [{
provider: RouterURLSerializer,
useClass: MyCustomSerializer
}]);
// URL
/team/3/details
// Activated routes
new ActivatedRoute(component: TeamCmp, url: [
new UrlPathWithParams(path: 'team', parameters: {}),
new UrlPathWithParams(path: '3', parameters: {})
])
new ActivatedRoute(component: DetailsCmp, url: [
new UrlPathWithParams(path: 'details', parameters: {})
])
// RouterState
// ActivatedRoute(component: RootCmp)
// -> ActivatedRoute(component: TeamCmp)
// -> ActivatedRoute(component: DetailsCmp)
// URL
/team/3(aux:/chat;open=true)
// Activated routes
new ActivatedRoute(component: TeamCmp, outlet: primary, url: [
new UrlPathWithParams(path: 'team', parameters: {}),
new UrlPathWithParams(path: '3', parameters: {})
])
new ActivatedRoute(component: ChatCmp, outlet: 'aux', url: [
new UrlPathWithParams(path: 'chat', parameters: {open: 'true'})
])
// RouterState
// ActivatedRoute(component: RootCmp, outlet: primary)
// -> ActivatedRoute(component: TeamCmp, outlet: primary)
// -> ActivatedRoute(component: ChatCmp, outlet: aux)
// Components:
@Component({
selector: chat,
template: `
Chat
`
})
class ChatComponent {}
@Component({
selector: 'team',
template: `
Team
primary: { <router-outlet></router-outlet> }
`
})
class TeamComponent {}
@Component({
selector: 'root',
template: `
Root
primary: { <router-outlet></router-outlet> }
aux: { <router-outlet name='aux'></router-outlet> }
`
})
class RootComponent {}
// Activated Route:
ActivatedRoute(component: RootComponent)
-> ActivatedRoute(component: TeamComponent, parameters: {id: 3}, outlet: primary)
-> ActivatedRoute(component: DetailsComponent, parameters: {}, outlet: primary)
-> ActivatedRoute(component: ChatComponent, parameters: {}, outlet: aux)
@Component({
selector: 'team',
template: `
Team Id: {{id | async}}
primary: { <router-outlet></router-outlet> }
`
})
class TeamComponent {
id: Observable<string>;
constructor(r: ActivatedRoute) {
//r.params is an observable
this.id = r.params.map(r => r.id);
}
}
@Component({
selector: 'details',
template: `
Details for Team Id: {{teamId | async}}
`
})
class DetailsComponent {
teamId:Observable<string>;
constructor(r: ActivatedRoute, router: Router) {
const teamActivatedRoute = router.routerState.parent(r);
this.teamId = teamActivatedRoute.params.map(r => r.id);
}
}
@Component({
selector: 'team',
template: ` `
})
class MyComponent {
constructor(r: Router) {
const q: Observable<{[k:string]:string}> = r.routerState.queryParams;
const f: Observable<string> = r.routerState.fragment;
}
}
@Component({
selector: 'team',
template: `
Team Id: {{id}}
`
})
class TeamComponent {
id:string;
constructor(r: ActivatedRoute, router: Router) {
const s: ActivatedRouteSnapshot = r.snapshot;
// matrix params of a particular route
this.id = s.params.id;
const ss: RouterStateSnapshot = router.routerState.snapshot;
// query params are shared
const q: {[k:string]:string} = ss.queryParams;
}
}
class TeamComponent {
teamId: number;
userName: string;
constructor(private router: Router) {}
onClick(e) {
this.router.navigate(['/team', this.teamId, 'user', this.userName]).then(_ => {
//navigation is done
}); //e.g. /team/3/user/victor
}
}
class TeamComponent {
private teamId;
private userName;
constructor(private router: Router, private r: ActivatedRoute) {}
onClick(e) {
this.router.navigate(['../', this.teamId, 'user', this.userName], {relativeTo: this.r});
}
}
@Component({
selector: 'team',
directives: ROUTER_DIRECTIVES,
template: `
<a [routerLink]="['../', this.teamId, 'user', this.userName]">Navigate</a>
`
})
class TeamComponent {
private teamId;
private userName;
}
// absolute navigation
this.router.navigate(['/team', this.teamId, 'details']);
// you can collapse static parts into a single element
this.router.navigate(['/team/3/details']);
// also set query params and fragment
this.router.navigate(['/team/3/details'], {queryParams: newParams, fragment: 'fragment'});
// e.g., /team/3;extra=true/details
this.router.navigate(['/team/3', {extra: true}, 'details']);
// relative navigation to /team/3/details
this.router.navigate(['./details'], {relativeTo: this.route});
// relative navigation to /team/3
this.router.navigate(['../', this.teamId], {relativeTo: this.route});