普通DI的抽象类在注入Store <state>

时间:2018-02-20 14:10:35

标签: angular typescript ngrx-store

我有一个 LoginCommon 类,它使用 Injector 将所有常见依赖项注入多个登录组件。此方法适用于除商店服务( @ ngrx / store )之外的所有依赖项。 当尝试使用此技术注入存储时,我得到运行时错误,如下所示:

Error: Token must be defined!

这个错误在依赖注入中看起来很基本,我缺少什么?

LoginCommon 类如下所示:

export class LoginCommon {
protected _loginAuth:LoginAuthService;
protected store: Store<State>;
protected _router:Router;
protected _route:ActivatedRoute;
constructor(
    injector: Injector
) {
        this._loginAuth= injector.get(LoginAuthService);
        this.store = injector.get(this.store);
        this._router = injector.get(Router);
        this._route = injector.get(ActivatedRoute);
    }
}

商店服务的对象如下所示:

export interface State {
    usermail:string;
    userphone:string;
    user_verification_code:string;
    userpassword:string;
}

扩展类构造函数如下所示:

export class LoginWithPasswordComponent extends LoginCommon {
...
    constructor(
        public injector: Injector
    ) {
        super(injector);
    }
...
}

注意!当从 LoginCommon 类注释掉商店注入,并像往常一样将其添加到 LoginWithPasswordComponent 构造函数时,一切正常。

1 个答案:

答案 0 :(得分:0)

我的bvehalf上的新秀错误。 传递了可靠的类型声明而不是服务实例。

this.store = injector.get(this.store);

应该是

this.store = injector.get(Store);

更改为上述问题解决了这个问题