refactor(core): change module semantics

This contains major changes to the compiler, bootstrap of the platforms
and test environment initialization.

Main part of #10043
Closes #10164

BREAKING CHANGE:
- Semantics and name of `@AppModule` (now `@NgModule`) changed quite a bit.
  This is actually not breaking as `@AppModules` were not part of rc.4.
  We will have detailed docs on `@NgModule` separately.
- `coreLoadAndBootstrap` and `coreBootstrap` can't be used any more (without migration support).
  Use `bootstrapModule` / `bootstrapModuleFactory` instead.
- All Components listed in routes have to be part of the `declarations` of an NgModule.
  Either directly on the bootstrap module / lazy loaded module, or in an NgModule imported by them.
This commit is contained in:
Tobias Bosch
2016-07-18 03:50:31 -07:00
parent ca16fc29a6
commit 46b212706b
129 changed files with 3580 additions and 3366 deletions

View File

@ -7,7 +7,7 @@
*/
import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';
import {ANALYZE_FOR_PRECOMPILE, APP_INITIALIZER, AppModuleFactoryLoader, ApplicationRef, ComponentResolver, Injector, OpaqueToken, SystemJsAppModuleLoader} from '@angular/core';
import {ANALYZE_FOR_PRECOMPILE, APP_INITIALIZER, ApplicationRef, ComponentResolver, Injector, NgModuleFactoryLoader, OpaqueToken, SystemJsNgModuleLoader} from '@angular/core';
import {Routes} from './config';
import {Router} from './router';
@ -26,7 +26,7 @@ export interface ExtraOptions { enableTracing?: boolean; }
export function setupRouter(
ref: ApplicationRef, resolver: ComponentResolver, urlSerializer: UrlSerializer,
outletMap: RouterOutletMap, location: Location, injector: Injector,
loader: AppModuleFactoryLoader, config: Routes, opts: ExtraOptions) {
loader: NgModuleFactoryLoader, config: Routes, opts: ExtraOptions) {
if (ref.componentTypes.length == 0) {
throw new Error('Bootstrap at least one component before injecting Router.');
}
@ -100,7 +100,7 @@ export function provideRouter(routes: Routes, config: ExtraOptions): any[] {
useFactory: setupRouter,
deps: [
ApplicationRef, ComponentResolver, UrlSerializer, RouterOutletMap, Location, Injector,
AppModuleFactoryLoader, ROUTES, ROUTER_CONFIGURATION
NgModuleFactoryLoader, ROUTES, ROUTER_CONFIGURATION
]
},
@ -108,7 +108,7 @@ export function provideRouter(routes: Routes, config: ExtraOptions): any[] {
// Trigger initial navigation
{provide: APP_INITIALIZER, multi: true, useFactory: setupRouterInitializer, deps: [Injector]},
{provide: AppModuleFactoryLoader, useClass: SystemJsAppModuleLoader}
{provide: NgModuleFactoryLoader, useClass: SystemJsNgModuleLoader}
];
}
@ -118,7 +118,7 @@ export function provideRouter(routes: Routes, config: ExtraOptions): any[] {
* ### Example
*
* ```
* @AppModule({providers: [
* @NgModule({providers: [
* provideRoutes([{path: 'home', component: Home}])
* ]})
* class LazyLoadedModule {
@ -141,7 +141,7 @@ export function provideRoutes(routes: Routes): any {
* ### Example
*
* ```
* @AppModule({providers: [
* @NgModule({providers: [
* provideRouterOptions({enableTracing: true})
* ]})
* class LazyLoadedModule {

View File

@ -41,8 +41,7 @@ function resolveNode(
function resolveComponent(
resolver: ComponentResolver, snapshot: ActivatedRouteSnapshot): Promise<any> {
// TODO: vsavkin change to typeof snapshot.component === 'string' in beta2
if (snapshot.component && snapshot._routeConfig) {
if (snapshot.component && snapshot._routeConfig && typeof snapshot.component === 'string') {
return resolver.resolveComponent(<any>snapshot.component);
} else {
return Promise.resolve(null);

View File

@ -13,7 +13,7 @@ import 'rxjs/add/operator/reduce';
import 'rxjs/add/operator/every';
import {Location} from '@angular/common';
import {AppModuleFactoryLoader, ComponentFactoryResolver, ComponentResolver, Injector, ReflectiveInjector, Type} from '@angular/core';
import {ComponentFactoryResolver, ComponentResolver, Injector, NgModuleFactoryLoader, ReflectiveInjector, Type} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {Subject} from 'rxjs/Subject';
import {Subscription} from 'rxjs/Subscription';
@ -146,7 +146,7 @@ export class Router {
constructor(
private rootComponentType: Type, private resolver: ComponentResolver,
private urlSerializer: UrlSerializer, private outletMap: RouterOutletMap,
private location: Location, private injector: Injector, loader: AppModuleFactoryLoader,
private location: Location, private injector: Injector, loader: NgModuleFactoryLoader,
config: Routes) {
this.resetConfig(config);
this.routerEvents = new Subject<Event>();

View File

@ -6,13 +6,14 @@
* found in the LICENSE file at https://angular.io/license
*/
import {AppModuleFactoryLoader, ComponentFactoryResolver, Injector, OpaqueToken} from '@angular/core';
import {ComponentFactoryResolver, Injector, NgModuleFactoryLoader, OpaqueToken} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {fromPromise} from 'rxjs/observable/fromPromise';
import {Route} from './config';
/**
* @deprecated use Routes
*/
@ -26,7 +27,7 @@ export class LoadedRouterConfig {
}
export class RouterConfigLoader {
constructor(private loader: AppModuleFactoryLoader) {}
constructor(private loader: NgModuleFactoryLoader) {}
load(parentInjector: Injector, path: string): Observable<LoadedRouterConfig> {
return fromPromise(this.loader.load(path).then(r => {
@ -35,4 +36,4 @@ export class RouterConfigLoader {
ref.injector.get(ROUTES), ref.injector, ref.componentFactoryResolver);
}));
}
}
}

View File

@ -7,7 +7,7 @@
*/
import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';
import {AppModule, AppModuleFactoryLoader, ApplicationRef, ComponentResolver, Injector, OpaqueToken, SystemJsAppModuleLoader} from '@angular/core';
import {ApplicationRef, ComponentResolver, Injector, NgModule, NgModuleFactoryLoader, OpaqueToken, SystemJsNgModuleLoader} from '@angular/core';
import {ROUTER_CONFIGURATION, rootRoute, setupRouter} from './common_router_providers';
import {RouterLink, RouterLinkWithHref} from './directives/router_link';
@ -33,14 +33,35 @@ export const ROUTER_PROVIDERS: any[] = [
useFactory: setupRouter,
deps: [
ApplicationRef, ComponentResolver, UrlSerializer, RouterOutletMap, Location, Injector,
AppModuleFactoryLoader, ROUTES, ROUTER_CONFIGURATION
NgModuleFactoryLoader, ROUTES, ROUTER_CONFIGURATION
]
},
RouterOutletMap, {provide: ActivatedRoute, useFactory: rootRoute, deps: [Router]},
{provide: AppModuleFactoryLoader, useClass: SystemJsAppModuleLoader},
{provide: NgModuleFactoryLoader, useClass: SystemJsNgModuleLoader},
{provide: ROUTER_CONFIGURATION, useValue: {enableTracing: false}}
];
/**
* Router module to be used for lazy loaded parts.
*
* ### Example
*
* ```
* @NgModule({
* imports: [RouterModuleWithoutProviders]
* })
* class TeamsModule {}
* ```
*
* @experimental We will soon have a way for the `RouterModule` to be imported with and without a
* provider,
* and then this module will be removed.
*/
@NgModule({declarations: ROUTER_DIRECTIVES, exports: ROUTER_DIRECTIVES})
export class RouterModuleWithoutProviders {
}
/**
* Router module.
*
@ -52,7 +73,7 @@ export const ROUTER_PROVIDERS: any[] = [
*
* @experimental
*/
@AppModule({directives: ROUTER_DIRECTIVES, providers: ROUTER_PROVIDERS})
@NgModule({exports: [RouterModuleWithoutProviders], providers: ROUTER_PROVIDERS})
export class RouterModule {
constructor(private injector: Injector) {
setTimeout(() => {
@ -64,4 +85,4 @@ export class RouterModule {
}
}, 0);
}
}
}