Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular2将来自AppModule的服务注入到从属NgModule';运行时是什么组件?_Javascript_Angular_Service_Dependency Injection_Module - Fatal编程技术网

Javascript Angular2将来自AppModule的服务注入到从属NgModule';运行时是什么组件?

Javascript Angular2将来自AppModule的服务注入到从属NgModule';运行时是什么组件?,javascript,angular,service,dependency-injection,module,Javascript,Angular,Service,Dependency Injection,Module,如果我有: AppModule 导入:[子模块], 提供者:[AppProvidedService] 及 子模块 声明:[子组件] 及 子组件 构造函数(appProvidedService:appProvidedService){} 我得到: Uncaught无法解析子组件(?)的所有参数。 基本上,它是说AppProvidedService不能由注入器解决 如何构建NgModule,使其依赖于在依赖它的NgModule中创建的服务 我尝试使用OpaqueToken和接口,但仍然存在相同

如果我有:

AppModule
导入:[子模块],
提供者:[AppProvidedService]

子模块
声明:[子组件]

子组件
构造函数(appProvidedService:appProvidedService){}

我得到:

Uncaught无法解析子组件(?)的所有参数。

基本上,它是说AppProvidedService不能由注入器解决

如何构建NgModule,使其依赖于在依赖它的NgModule中创建的服务

我尝试使用OpaqueToken和接口,但仍然存在相同的问题。子模块无法看到来自父模块的内容


最终的愿望是拥有一个模块,可以从导入它的应用程序中进行注入。这样,我就可以将特定于应用程序的行为注入到通用组件中。

我发现了这一点。它确实需要不透明的标记

首先,为接口提供接口和OpaqueToken(app-provided.service.interface:

import { OpaqueToken } from "@angular/core";

export interface AppProvidedServiceInterface {
    ...
}

export const SERVICE_INTERFACE = new OpaqueToken("AppProvidedServiceInterface");
在AppModule的提供程序中:

    /*
     * Injection of app-specific behavior into a common module without hard dependencies:
     *
     * This is how you inject a service into a module that is imported
     * and prevent that module from having a hard dependency on the actual
     * implementation.  Use an interface with an OpaqueToken. Then the
     * component that needs it imports it and uses the AppInjector to get the
     * instance.
     */
    {
        provide: SERVICE_INTERFACE,
        useFactory: () => {
            return new AppProvidedService();
        }
    }
在bootstrap中,获取对AppInjector的引用并将其存储…(我们使用一个名为AppInjector的简单单例类(答案末尾的源代码)):

现在,您的子模块引用了一个接口,而不是具体的实现,AppModule可以通过
提供程序:[]
注入所需的行为

AppInjector单例:

import { Injector } from "@angular/core";

export class AppInjector {
    private static instance: AppInjector;
    private injector: Injector;

    constructor() {
        // throw new Error("Singleton - can't instantiate.");        
    }

    public static getInstance() {
        if (!AppInjector.instance) {
            AppInjector.instance = new AppInjector();
        }

        return AppInjector.instance;
    }

    public setInjector(injector: Injector) {
        this.injector = injector;
    }

    public getInjector(): Injector {
        return this.injector;
    }
}

这仅仅是一个输入错误吗?您使用的是
提供程序
而不是
提供程序
?这种方法应该会起作用。我有一些问号问题转移到模块方法,但能够解决所有问题。不幸的是,我不记得如何)您需要将正确的内容放在正确的位置。是的,也解决所有输入错误。)
// DO NOT REFERENCE A CONCRETE SERVICE!
private appProvidedService: AppProvidedServiceInterface;

constructor(/* can't inject it here */) {
    // Get the app-specific behavior from the service injected in the application module.
    this.appProvidedService = AppInjector.getInstance().getInjector().get(SERVICE_INTERFACE);
}
import { Injector } from "@angular/core";

export class AppInjector {
    private static instance: AppInjector;
    private injector: Injector;

    constructor() {
        // throw new Error("Singleton - can't instantiate.");        
    }

    public static getInstance() {
        if (!AppInjector.instance) {
            AppInjector.instance = new AppInjector();
        }

        return AppInjector.instance;
    }

    public setInjector(injector: Injector) {
        this.injector = injector;
    }

    public getInjector(): Injector {
        return this.injector;
    }
}