ngrx / store将商店注入函数

时间:2016-09-10 15:06:21

标签: angular store ngrx

我知道我可以在这样的组件构造函数中注入store:

constructor(private store:Store){

   store.select('thing').subscribe(
    ()=>{ 
          console.log('do my thing'); // Got the thing
         }
   );
}

但我想创建一个装饰器,以便能够在其中使用此选择,所以我想在我的装饰器中注入Store:

export function Select<T> ( selector ) {
    return function decorate ( target : any , key : string ) : void {
        let injector = ReflectiveInjector.resolveAndCreate( [
            StoreModule.provideStore(()=>{} , {}).providers
        ] );
        let store    = injector.get( Store );
        store.select( 'thing' ).subscribe( ()=> {
            console.log( 'do my thing',thing ); // thing is undefined
        } );
    }
}

奇怪的是Store不是一个Injectable类(查看源代码),它需要传递3个属性才能实例化它(Reducers,State和一个)。

那么Angular本身如何在构造函数中注入该类,而不提供这些参数,但是当我尝试注入时,它需要它们?

我真的尝试过所有不同的Injector和ReflectiveInjector方法,但它们都不适合我。

0 个答案:

没有答案
相关问题