feat(core): introduce ModuleWithProviders.
Modules can now provider helper functions that allow to import a module together with an array of providers. Part of #10043
This commit is contained in:
@@ -16,13 +16,13 @@ import {ChangeDetectionStrategy} from '../src/change_detection/change_detection'
|
||||
import {AnimationEntryMetadata} from './animation/metadata';
|
||||
import {AttributeMetadata, ContentChildMetadata, ContentChildrenMetadata, QueryMetadata, ViewChildMetadata, ViewChildrenMetadata, ViewQueryMetadata} from './metadata/di';
|
||||
import {ComponentMetadata, DirectiveMetadata, HostBindingMetadata, HostListenerMetadata, InputMetadata, OutputMetadata, PipeMetadata} from './metadata/directives';
|
||||
import {NgModuleMetadata} from './metadata/ng_module';
|
||||
import {ModuleWithProviders, NgModuleMetadata} from './metadata/ng_module';
|
||||
import {ViewEncapsulation, ViewMetadata} from './metadata/view';
|
||||
|
||||
export {ANALYZE_FOR_ENTRY_COMPONENTS, AttributeMetadata, ContentChildMetadata, ContentChildrenMetadata, QueryMetadata, ViewChildMetadata, ViewChildrenMetadata, ViewQueryMetadata} from './metadata/di';
|
||||
export {ComponentMetadata, DirectiveMetadata, HostBindingMetadata, HostListenerMetadata, InputMetadata, OutputMetadata, PipeMetadata} from './metadata/directives';
|
||||
export {AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, DoCheck, OnChanges, OnDestroy, OnInit} from './metadata/lifecycle_hooks';
|
||||
export {NgModuleMetadata} from './metadata/ng_module';
|
||||
export {ModuleWithProviders, NgModuleMetadata} from './metadata/ng_module';
|
||||
export {ViewEncapsulation, ViewMetadata} from './metadata/view';
|
||||
|
||||
import {makeDecorator, makeParamDecorator, makePropDecorator, TypeDecorator,} from './util/decorators';
|
||||
@@ -498,7 +498,7 @@ export interface NgModuleMetadataFactory {
|
||||
(obj?: {
|
||||
providers?: any[],
|
||||
declarations?: Array<Type|any[]>,
|
||||
imports?: Array<Type|any[]>,
|
||||
imports?: Array<Type|ModuleWithProviders|any[]>,
|
||||
exports?: Array<Type|any[]>,
|
||||
entryComponents?: Array<Type|any[]>
|
||||
}): NgModuleDecorator;
|
||||
|
||||
@@ -9,6 +9,16 @@
|
||||
import {InjectableMetadata} from '../di/metadata';
|
||||
import {Type} from '../facade/lang';
|
||||
|
||||
/**
|
||||
* A wrapper around a module that also includes the providers.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export interface ModuleWithProviders {
|
||||
ngModule: Type;
|
||||
providers?: any[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Declares an Angular Module.
|
||||
* @experimental
|
||||
@@ -65,6 +75,7 @@ export class NgModuleMetadata extends InjectableMetadata {
|
||||
/**
|
||||
* Specifies a list of modules whose exported directives/pipes
|
||||
* should be available to templates in this module.
|
||||
* This can also contain {@link ModuleWithProviders}.
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
@@ -76,7 +87,7 @@ export class NgModuleMetadata extends InjectableMetadata {
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
imports: Array<Type|any[]>;
|
||||
imports: Array<Type|ModuleWithProviders|any[]>;
|
||||
|
||||
/**
|
||||
* Specifies a list of directives/pipes/module that can be used within the template
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
import {LowerCasePipe, NgIf} from '@angular/common';
|
||||
import {CompilerConfig, NgModuleResolver, ViewResolver} from '@angular/compiler';
|
||||
import {MockNgModuleResolver, MockViewResolver} from '@angular/compiler/testing';
|
||||
import {ANALYZE_FOR_ENTRY_COMPONENTS, Compiler, Component, ComponentFactoryResolver, ComponentRef, ComponentResolver, DebugElement, Directive, Host, HostBinding, Inject, Injectable, Injector, Input, NgModule, NgModuleMetadata, NgModuleRef, OpaqueToken, Optional, Pipe, Provider, ReflectiveInjector, SelfMetadata, SkipSelf, SkipSelfMetadata, ViewMetadata, forwardRef, getDebugNode, provide} from '@angular/core';
|
||||
import {ANALYZE_FOR_ENTRY_COMPONENTS, Compiler, Component, ComponentFactoryResolver, ComponentRef, ComponentResolver, DebugElement, Directive, Host, HostBinding, Inject, Injectable, Injector, Input, ModuleWithProviders, NgModule, NgModuleMetadata, NgModuleRef, OpaqueToken, Optional, Pipe, Provider, ReflectiveInjector, SelfMetadata, SkipSelf, SkipSelfMetadata, ViewMetadata, forwardRef, getDebugNode, provide} from '@angular/core';
|
||||
import {Console} from '@angular/core/src/console';
|
||||
import {ComponentFixture, configureCompiler} from '@angular/core/testing';
|
||||
import {AsyncTestCompleter, beforeEach, beforeEachProviders, ddescribe, describe, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal';
|
||||
@@ -449,6 +449,28 @@ function declareTests({useJit}: {useJit: boolean}) {
|
||||
.toBe('transformed someValue');
|
||||
});
|
||||
|
||||
it('should support exported directives and pipes if the module is wrapped into an `ModuleWithProviders`',
|
||||
() => {
|
||||
@NgModule(
|
||||
{declarations: [SomeDirective, SomePipe], exports: [SomeDirective, SomePipe]})
|
||||
class SomeImportedModule {
|
||||
}
|
||||
|
||||
@NgModule({
|
||||
declarations: [CompUsingModuleDirectiveAndPipe],
|
||||
imports: [{ngModule: SomeImportedModule}],
|
||||
entryComponents: [CompUsingModuleDirectiveAndPipe]
|
||||
})
|
||||
class SomeModule {
|
||||
}
|
||||
|
||||
|
||||
const compFixture = createComp(CompUsingModuleDirectiveAndPipe, SomeModule);
|
||||
compFixture.detectChanges();
|
||||
expect(compFixture.debugElement.children[0].properties['title'])
|
||||
.toBe('transformed someValue');
|
||||
});
|
||||
|
||||
it('should support reexported modules', () => {
|
||||
@NgModule({declarations: [SomeDirective, SomePipe], exports: [SomeDirective, SomePipe]})
|
||||
class SomeReexportedModule {
|
||||
@@ -876,6 +898,26 @@ function declareTests({useJit}: {useJit: boolean}) {
|
||||
expect(injector.get('token1')).toBe('imported');
|
||||
});
|
||||
|
||||
it('should add the providers of imported ModuleWithProviders', () => {
|
||||
@NgModule()
|
||||
class ImportedModule {
|
||||
}
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
{ngModule: ImportedModule, providers: [{provide: 'token1', useValue: 'imported'}]}
|
||||
]
|
||||
})
|
||||
class SomeModule {
|
||||
}
|
||||
|
||||
const injector = createModule(SomeModule).injector;
|
||||
|
||||
expect(injector.get(SomeModule)).toBeAnInstanceOf(SomeModule);
|
||||
expect(injector.get(ImportedModule)).toBeAnInstanceOf(ImportedModule);
|
||||
expect(injector.get('token1')).toBe('imported');
|
||||
});
|
||||
|
||||
it('should overwrite the providers of imported modules', () => {
|
||||
@NgModule({providers: [{provide: 'token1', useValue: 'imported'}]})
|
||||
class ImportedModule {
|
||||
|
||||
Reference in New Issue
Block a user