相关疑难解决方法(0)

继承和依赖注入

我有一组angular2组件,应该都会注入一些服务.我的第一个想法是,最好创建一个超级类并在那里注入服务.然后我的任何组件都会扩展该超类,但这种方法不起作用.

简化示例:

export class AbstractComponent {
  constructor(private myservice: MyService) {
    // Inject the service I need for all components
  }
}

export MyComponent extends AbstractComponent {
  constructor(private anotherService: AnotherService) {
    super(); // This gives an error as super constructor needs an argument
  }
}
Run Code Online (Sandbox Code Playgroud)

我可以通过MyService在每个组件中注入并使用该参数进行super()调用来解决这个问题,但这肯定是某种荒谬的.

如何正确组织我的组件,以便他们从超类继承服务?

inheritance inject typescript typescript1.8 angular

76
推荐指数
4
解决办法
3万
查看次数

存储注入器实例以用于组件

在RC5之前我使用appref injector作为服务定位器,如下所示:

Startup.ts

bootstrap(...)
.then((appRef: any) => {
    ServiceLocator.injector = appRef.injector;
});
Run Code Online (Sandbox Code Playgroud)

ServiceLocator.ts

export class ServiceLocator {
    static injector: Injector;
}
Run Code Online (Sandbox Code Playgroud)

组件:

let myServiceInstance = <MyService>ServiceLocator.injector.get(MyService)
Run Code Online (Sandbox Code Playgroud)

现在在bootstrapModule()中执行相同操作.然后()不起作用,因为组件似乎在promise之前开始执行.

有没有办法在组件加载之前存储喷油器实例?

我不想因为我使用其中的许多组件衍生,我宁可不注射器注入到所有这些基本成分的注射器使用构造器注入.

angular

23
推荐指数
2
解决办法
9587
查看次数

Angular 2/4/5/6模型逻辑

所以这是一个非常抽象的例子,但我们采用以下模型和服务定义:

模型/对象定义 - car.ts

... /*imports*/
export class Car{
    color: string;
    wheels: number;
    oilChangeDate: Date;
    tyreChangeDate: Date;

... /*Additional properties*/

}
Run Code Online (Sandbox Code Playgroud)

服务 - car.service.ts

@Injectable()
export class CarService{
    currentCar: Car;

    constructor(http: Http){
    ... /*Initialization*/
    }

    ...

    serviceCurrentVehicle(){
        currentCar.oilChangeDate = new Date();
        currentCar.tyreChangeDate = new Date();
        /* Some http request */
    }
}
Run Code Online (Sandbox Code Playgroud)

问题

  1. 是否违反Angular 2/4样式/设计指南将serviceCurrentVehicle背后的逻辑移动到模型定义?为什么?
  2. 是否通常将Angular 2/4样式/设计指南与模型定义相关联?如果没有,在维护对象的构造函数的同时如何访问服务?(即没有依赖注入)

在创建此问题之前,我已查看类似于此的帖子.

编辑:早些时候也看过OAuth上的这篇文章,这是我打算创建的东西,但是(a)教程不包括在保存用户时调用HTTP请求的代码和(b)我可以在官方文档/样式指南中找不到具有业务逻辑的这种域模型的任何支持引用.

干杯

编辑2:所以我创建这个已经超过一年了,但是我还没有找到任何体面的资源.我在我们的系统中实现了一个模型定义,我正在寻找方法来改进它,因为它是企业级的.

service dependency-injection angular

5
推荐指数
0
解决办法
316
查看次数

Angular - 以构造函数以外的其他方式注入?

在 Angular 中,我有一个服务,通过constructor(...). 然而,该服务也是在某个地方通过调用构造函数创建的。因此,在参数中添加它所依赖的另一个服务将会改变 API。我想避免这种情况。

有没有办法将一个服务注入另一个服务而不将其添加到构造函数参数中?例如现场注入?

import {Inject, Injectable} from "@angular/core";
import {
    Http, Request, ConnectionBackend, RequestOptions, RequestOptionsArgs, Response, Headers,
    RequestMethod
} from "@angular/http";

import {KeycloakService} from "./keycloak.service";
import {Observable} from 'rxjs/Observable';
import {EventBusService} from "../events/event-bus.service";
import {LoadingSomethingFinishedEvent, LoadingSomethingStartedEvent} from "../events/windup-event";

@Injectable()
export class WindupHttpService extends Http {
    constructor(
        _backend: ConnectionBackend,
        _defaultOptions: RequestOptions,
        private _keycloakService: KeycloakService,
        // ----- This is what I want to avoid. -----
        private _eventBus: EventBusService,
    ) {
        super(_backend, _defaultOptions);
    }

    // -------  This is …
Run Code Online (Sandbox Code Playgroud)

typescript angular-services angular2-services angular

3
推荐指数
1
解决办法
9854
查看次数