fix(router): provide a top-level route segment for injection

This commit is contained in:
vsavkin 2016-05-04 14:40:17 -07:00
parent d00b26d941
commit b8136cc26e
6 changed files with 14 additions and 3 deletions

View File

@ -59,7 +59,7 @@ export class RouterLink implements OnDestroy {
@HostBinding() href: string; @HostBinding() href: string;
@HostBinding('class.router-link-active') isActive: boolean = false; @HostBinding('class.router-link-active') isActive: boolean = false;
constructor(@Optional() private _routeSegment: RouteSegment, private _router: Router) { constructor(private _routeSegment: RouteSegment, private _router: Router) {
// because auxiliary links take existing primary and auxiliary routes into account, // because auxiliary links take existing primary and auxiliary routes into account,
// we need to update the link whenever params or other routes change. // we need to update the link whenever params or other routes change.
this._subscription = this._subscription =

View File

@ -8,7 +8,6 @@ import {ComponentResolver} from '@angular/core';
import {DEFAULT_OUTLET_NAME} from './constants'; import {DEFAULT_OUTLET_NAME} from './constants';
import {reflector} from '@angular/core'; import {reflector} from '@angular/core';
// TODO: vsavkin: recognize should take the old tree and merge it
export function recognize(componentResolver: ComponentResolver, rootComponent: Type, export function recognize(componentResolver: ComponentResolver, rootComponent: Type,
url: UrlTree): Promise<RouteTree> { url: UrlTree): Promise<RouteTree> {
let matched = new _MatchResult(rootComponent, [url.root], {}, rootNode(url).children, []); let matched = new _MatchResult(rootComponent, [url.root], {}, rootNode(url).children, []);

View File

@ -1,6 +1,7 @@
import {OpaqueToken, ComponentResolver} from '@angular/core'; import {OpaqueToken, ComponentResolver} from '@angular/core';
import {LocationStrategy, PathLocationStrategy, Location} from '@angular/common'; import {LocationStrategy, PathLocationStrategy, Location} from '@angular/common';
import {Router, RouterOutletMap} from './router'; import {Router, RouterOutletMap} from './router';
import {RouteSegment} from './segments';
import {RouterUrlSerializer, DefaultRouterUrlSerializer} from './router_url_serializer'; import {RouterUrlSerializer, DefaultRouterUrlSerializer} from './router_url_serializer';
import {ApplicationRef} from '@angular/core'; import {ApplicationRef} from '@angular/core';
import {BaseException} from '@angular/core'; import {BaseException} from '@angular/core';
@ -18,6 +19,7 @@ export const ROUTER_PROVIDERS_COMMON: any[] = /*@ts2dart_const*/[
deps: /*@ts2dart_const*/ deps: /*@ts2dart_const*/
[ApplicationRef, ComponentResolver, RouterUrlSerializer, RouterOutletMap, Location], [ApplicationRef, ComponentResolver, RouterUrlSerializer, RouterOutletMap, Location],
}, },
/*@ts2dart_Provider*/ {provide: RouteSegment, useFactory: (r) => r.routeTree.root, deps: [Router]}
]; ];
function routerFactory(app: ApplicationRef, componentResolver: ComponentResolver, function routerFactory(app: ApplicationRef, componentResolver: ComponentResolver,

View File

@ -39,6 +39,7 @@ export function main() {
provide(RouterUrlSerializer, {useClass: DefaultRouterUrlSerializer}), provide(RouterUrlSerializer, {useClass: DefaultRouterUrlSerializer}),
RouterOutletMap, RouterOutletMap,
provide(Location, {useClass: SpyLocation}), provide(Location, {useClass: SpyLocation}),
provide(RouteSegment, {useFactory: (r) => r.routeTree.root, deps: [Router]}),
provide(Router, provide(Router,
{ {
useFactory: (resolver, urlParser, outletMap, location) => new Router( useFactory: (resolver, urlParser, outletMap, location) => new Router(
@ -224,6 +225,13 @@ export function main() {
expect(getDOM().getAttribute(native, "href")).toEqual("/team/33/simple(aux:simple2)"); expect(getDOM().getAttribute(native, "href")).toEqual("/team/33/simple(aux:simple2)");
}))); })));
it("should support top-level link",
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
let fixture = tcb.createFakeAsync(LinkCmp);
advance(fixture);
expect(fixture.debugElement.nativeElement).toHaveText('link');
})));
} }
}); });
} }

View File

@ -20,7 +20,7 @@ import {DefaultRouterUrlSerializer} from '../src/router_url_serializer';
import {DEFAULT_OUTLET_NAME} from '../src/constants'; import {DEFAULT_OUTLET_NAME} from '../src/constants';
export function main() { export function main() {
ddescribe('recognize', () => { describe('recognize', () => {
it('should handle position args', it('should handle position args',
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => { inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
recognize(resolver, ComponentA, tree("b/paramB/c/paramC/d")) recognize(resolver, ComponentA, tree("b/paramB/c/paramC/d"))

View File

@ -1,6 +1,7 @@
import {SpyLocation} from '@angular/common/testing'; import {SpyLocation} from '@angular/common/testing';
import {Location} from '@angular/common'; import {Location} from '@angular/common';
import {Router, RouterOutletMap} from '../src/router'; import {Router, RouterOutletMap} from '../src/router';
import {RouteSegment} from '../src/segments';
import {RouterUrlSerializer, DefaultRouterUrlSerializer} from '../src/router_url_serializer'; import {RouterUrlSerializer, DefaultRouterUrlSerializer} from '../src/router_url_serializer';
import {Component, ComponentResolver} from '@angular/core'; import {Component, ComponentResolver} from '@angular/core';
@ -24,4 +25,5 @@ export const ROUTER_FAKE_PROVIDERS: any[] = /*@ts2dart_const*/ [
deps: /*@ts2dart_const*/ deps: /*@ts2dart_const*/
[ComponentResolver, RouterUrlSerializer, RouterOutletMap, Location] [ComponentResolver, RouterUrlSerializer, RouterOutletMap, Location]
}, },
/*@ts2dart_Provider*/ {provide: RouteSegment, useFactory: (r) => r.routeTree.root, deps: [Router]}
]; ];