简体   繁体   English

Angular 6 在不使用构造函数的情况下将服务注入到类中

[英]Angular 6 injecting service into class without using the constructor

Using Angular 6 and Typescript 2.7.使用 Angular 6 和 Typescript 2.7。 I have a class with a constructor that I pass arguments to when I create an instance from a controller.我有一个带有构造函数的类,当我从控制器创建实例时,我将参数传递给它。 This class also has a service that needs to be injected.这个类还有一个需要注入的服务。 But then the controller that creates the instance needs to provide the service as a parameter.但是随后创建实例的控制器需要提供服务作为参数。 Is this common or is there a way to just inject the service in the class and not the constructor.这是常见的还是有一种方法可以将服务注入类而不是构造函数。 I have read in other posts that the constructor should only be used to inject dependencies.我在其他帖子中读到构造函数应该只用于注入依赖项。

So I have the configuration class that takes simple string parameters.所以我有一个采用简单字符串参数的配置类。

export class Configuration{
    //public uiService: UiService;
    public configName:string;
    public configType: ConfigType;
    public shape:any;

    constructor(_configName: string, _configType: ConfigType){
        this.configName = _configName;
        this.configType = _configType;
....

I create a new instance of this class from my controller like this:我从我的控制器创建这个类的一个新实例,如下所示:

let config = new Configuration("NEWconfiguration", configType);

I have a common service with utility functions that I need to use in the Configuration class.我有一个带有实用程序功能的公共服务,我需要在 Configuration 类中使用它。 Reading here and other sites I see that this should be injected through the constructor.阅读here和其他网站我看到这应该通过构造函数注入。 So then my configuration constructor would include a new parameter for the service and then the controller would inject it.因此,我的配置构造函数将为服务包含一个新参数,然后控制器将注入它。 But the service is not used in the controller so I don't want to have to include it there.但是该服务没有在控制器中使用,所以我不想把它包含在那里。

Is there a way in the Configuration class to access the service without using the constructor? Configuration 类中有没有办法在不使用构造函数的情况下访问服务? Or is this bad practice.或者这是不好的做法。 I think I could create an instance of the service within the Configuration class or make the functions in the service static but then I am not using dependency injection.我想我可以在 Configuration 类中创建一个服务实例,或者使服务中的函数成为静态,但我没有使用依赖注入。 Does that matter?那有关系吗?

I faced a similar issue recently, where I had to create multiple instances of the same class, which isn't possible with Angular DI, which creates singletons by default.我最近遇到了一个类似的问题,我必须创建同一个类的多个实例,而 Angular DI 则无法做到这一点,默认情况下它会创建单例。 The class instances did however have dependencies on other services in my application.然而,类实例确实依赖于我的应用程序中的其他服务。

The way I solved it was to inject the dependencies into my controller, and instantiate the classes using their constructors without DI.我解决它的方法是将依赖项注入我的控制器,并使用它们的构造函数来实例化这些类,而无需 DI。

Let me know if the above is sufficient, if not I can add example code to improve clarity.让我知道以上是否足够,如果不够,我可以添加示例代码以提高清晰度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 8 karma 测试一个服务而不在构造函数中注入所有服务 - Angular 8 karma test a service without injecting all service in the constructor Angular Service我们为什么要注入构造函数 - Angular Service Why are we injecting on constructor Angular4注入服务类-调用服务方法时构造函数中的代码未完成? - Angular4 Injecting Service class - code in constructor not finished when service method called? 在类构造函数中注入服务的长形式 - Long form of injecting a service in a class constructor 如何在不使用构造函数的情况下实例化angular 2服务? - How to instantiate an angular 2 service without using the constructor? Angular:使用构造函数注入服务的替代方法 - Angular: alternative to injecting services using the constructor 在没有构造函数的情况下将服务单例实例注入到另一个服务 - Injecting service singleton instance to another service without constructor 将服务注入另一个类-Angular2 - Injecting a service into another class - Angular2 Angular2将服务注入类错误 - Angular2 Injecting a service into Class error Angular 8:在不使用构造函数的情况下将服务实例获取到另一个服务 - Angular 8: Get instance of service to another service without using constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM