Angular2 component definition with additional conf

2019-09-19 05:32发布

问题:

I want to pass a provider right to a component.

@NgModule({
    declarations:[
        SomeComponent.withProviders([provider1,provider2]),
        AnotherComponent.withProviders([provider1,provider2])
    ]
})
export class NestedModule{}

My question: Can I configure my components while defining them in the module like above?

More information:

The reason do I need such a thing:

  • I need to define a value provider at module level.
  • The component looks at it's parent component to find say provider1 if it doesn't have one.
  • I need to define the value provider at the SomeComponent's module level (the NestedModule).
  • The parent component belongs to another module and it has a provider with the same key but with a different value at it's module level.
  • I want to declare the value provider in a single place not to
    redefine it for each component. Any other solution is welcome.