X Tutup
Skip to content

Commit d9036c6

Browse files
committed
feat(router): introduce new navigate method
Previously, `router.navigate` took a string representing the URL. Now, it accepts an array that mirrors the link DSL. Closes #4040 BREAKING CHANGE The old method has been renamed to `router.navigateByUrl`. Either change your navigation calls to use the DSL (preferred) or call `router.navigateByUrl` instead. Closes #4074
1 parent acc2722 commit d9036c6

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

modules/angular2/src/router/router.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,27 @@ export class Router {
152152
return this.renavigate();
153153
}
154154

155+
/**
156+
* Navigate based on the provided Route Link DSL. It's preferred to navigate with this method
157+
* over `navigateByUrl`.
158+
*
159+
* # Usage
160+
*
161+
* This method takes an array representing the Route Link DSL:
162+
* ```
163+
* ['./MyCmp', {param: 3}]
164+
* ```
165+
* See the {@link RouterLink} directive for more.
166+
*/
167+
navigate(linkParams: any[]): Promise<any> {
168+
var instruction = this.generate(linkParams);
169+
return this.navigateByInstruction(instruction, false);
170+
}
171+
155172

156173
/**
157174
* Navigate to a URL. Returns a promise that resolves when navigation is complete.
175+
* It's preferred to navigate with `navigate` instead of this method, since URLs are more brittle.
158176
*
159177
* If the given URL begins with a `/`, router will navigate absolutely.
160178
* If the given URL does not begin with `/`, the router will navigate relative to this component.

modules/angular2/test/router/integration/router_link_spec.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,7 @@ export function main() {
139139
as: 'child-with-grandchild'
140140
})
141141
]))
142-
.then((_) => {
143-
// TODO: refactor when https://github.com/angular/angular/pull/4074 lands
144-
var instruction = router.generate(['/child-with-grandchild']);
145-
return router.navigateInstruction(instruction);
146-
})
142+
.then((_) => router.navigate(['/child-with-grandchild']))
147143
.then((_) => {
148144
rootTC.detectChanges();
149145
expect(DOM.getAttribute(

modules/angular2/test/router/router_spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ export function main() {
7676
});
7777
}));
7878

79+
it('should activate viewports and update URL when navigating via DSL',
80+
inject([AsyncTestCompleter], (async) => {
81+
var outlet = makeDummyOutlet();
82+
83+
router.registerPrimaryOutlet(outlet)
84+
.then((_) =>
85+
router.config([new Route({path: '/a', component: DummyComponent, as: 'A'})]))
86+
.then((_) => router.navigate(['/A']))
87+
.then((_) => {
88+
expect(outlet.spy('activate')).toHaveBeenCalled();
89+
expect(location.urlChanges).toEqual(['/a']);
90+
async.done();
91+
});
92+
}));
93+
7994
it('should not push a history change on when navigate is called with skipUrlChange',
8095
inject([AsyncTestCompleter], (async) => {
8196
var outlet = makeDummyOutlet();

0 commit comments

Comments
 (0)
X Tutup