feat(core): Throw a descriptive error when BrowserModule is installed a second time (via lazy loading). (#10899)

Such a configuration is unsupported and causes all kinds of problems.
This commit is contained in:
Alex Rickabaugh 2016-08-18 13:34:28 -07:00 committed by Kara
parent 91980382e8
commit 628d06c17c
3 changed files with 26 additions and 2 deletions

View File

@ -7,7 +7,7 @@
*/
import {CommonModule, PlatformLocation} from '@angular/common';
import {ApplicationModule, ExceptionHandler, NgModule, PLATFORM_INITIALIZER, PlatformRef, Provider, RootRenderer, SanitizationService, Testability, createPlatformFactory, platformCore} from '@angular/core';
import {ApplicationModule, BaseException, ExceptionHandler, NgModule, Optional, PLATFORM_INITIALIZER, PlatformRef, Provider, RootRenderer, SanitizationService, SkipSelf, Testability, createPlatformFactory, platformCore} from '@angular/core';
import {wtfInit} from '../core_private';
import {AnimationDriver} from '../src/dom/animation_driver';
@ -93,4 +93,10 @@ export function _resolveDefaultAnimationDriver(): AnimationDriver {
exports: [CommonModule, ApplicationModule]
})
export class BrowserModule {
constructor(@Optional() @SkipSelf() parentModule: BrowserModule) {
if (parentModule) {
throw new BaseException(
`BrowserModule has already been loaded. If you need access to common directives such as NgIf and NgFor from a lazy loaded module, import CommonModule instead.`);
}
}
}

View File

@ -7,7 +7,7 @@
*/
import {ResourceLoader} from '@angular/compiler';
import {APP_INITIALIZER, CUSTOM_ELEMENTS_SCHEMA, Component, Directive, ExceptionHandler, Inject, Input, NgModule, OnDestroy, PLATFORM_INITIALIZER, Pipe, createPlatformFactory} from '@angular/core';
import {APP_INITIALIZER, CUSTOM_ELEMENTS_SCHEMA, Compiler, Component, Directive, ExceptionHandler, Inject, Input, NgModule, OnDestroy, PLATFORM_INITIALIZER, Pipe, createPlatformFactory} from '@angular/core';
import {ApplicationRef, destroyPlatform} from '@angular/core/src/application_ref';
import {Console} from '@angular/core/src/console';
import {ComponentRef} from '@angular/core/src/linker/component_factory';
@ -216,6 +216,23 @@ export function main() {
});
}));
it('should throw a descriptive error if BrowserModule is installed again via a lazily loaded module',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
@NgModule({imports: [BrowserModule]})
class AsyncModule {
}
bootstrap(HelloRootCmp, testProviders)
.then((ref: ComponentRef<HelloRootCmp>) => {
let compiler: Compiler = ref.injector.get(Compiler);
return compiler.compileModuleAsync(AsyncModule).then(factory => {
expect(() => factory.create(ref.injector))
.toThrowError(
`BrowserModule has already been loaded. If you need access to common directives such as NgIf and NgFor from a lazy loaded module, import CommonModule instead.`);
});
})
.then(() => async.done(), err => async.fail(err));
}));
it('should support multiple calls to bootstrap',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
var refPromise1 = bootstrap(HelloRootCmp, testProviders);

View File

@ -12,6 +12,7 @@ export declare const BROWSER_SANITIZATION_PROVIDERS: Array<any>;
/** @experimental */
export declare class BrowserModule {
constructor(parentModule: BrowserModule);
}
/** @stable */