Inject service in a simple class constructor with Angular2

I created a Message class like this

import { ReflectiveInjector } from '@angular/core';

import { ApiService } from '../api.service';

export class Message {
        timestamp: number;
        message: any;
        api: ApiService;

        constructor(message: any) {
                let injector = ReflectiveInjector.resolveAndCreate([ApiService]);
                this.api = injector.get(ApiService);
                this.timestamp = message.timestamp;
                this.message = message.message;
        }
}

I am not entering ApiService directly into the constructor parameters, because I try to avoid this: let nm = new Message(message, this.api)
I do not want the service to be in the parameters.

So, I am using ReflectiveInjector, but this code does not even work. I get this error: EXCEPTION: Error: Unprepared (in promise): There is no provider for Http! (ApiService → Http) , even if I turned on HTTP_PROVIDERS in this way

import { bootstrap } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { HTTP_PROVIDERS } from '@angular/http';

import { AppComponent, environment } from './app/';
import { appRouterProviders } from './app/app.routes';

if (environment.production) {
  enableProdMode();
}

bootstrap(AppComponent, [
        appRouterProviders,
        HTTP_PROVIDERS,
])
.catch(err => console.log(err));

How can I use the constructor to instantiate the class and implement my services: let nm = new Message(message);

thank

+4
source share
3 answers

,

let nm = new Message(message);

bootstrap(AppComponent, [ 
  APP_ROUTER_PROVIDERS,
  [provide(Message,{useValue:nm})],
]).catch(err => console.error(err));

: : ( ): Http! (ApiService → Http)

HTTP,

import { Http, Response } from '@angular/http';
export class LoginService {

  constructor(private http: Http) {

  }
}

, , . , , . , .

bootstrap(AppComponent, [ 
  APP_ROUTER_PROVIDERS,
  Message //you service here
]).catch(err => console.error(err));
+2

- , :

, ; , ? , Angular2 - - . , , () .

, :

@Injectable()
export class MessageFactory {
  constructor(private service: Service)

  build(data: any): Message {
    let message = new Message(data);
    message.service = this.service;
    return message;
  }
}

export class Message {
  service: Service;

  constructor(private data: any) {
    // Can't call service here but it okay for me...
  }

  doSomethingWithService(): {
    this.service.doSomething(this.data);
  }
}

, - MessageFactory :

export class MessageExampleComponent {
  constructor(private messageFactory: MessageFactory) {}

  makeMessageDoSomethingWithService(): {
    let message = this.messageFactory.build({just: 'an', example: 'here'})
    message.doSomethingWithService();
  }
}

, MessageFactory, , , - .

+2

, @Inject

import { Inject } from "@angular/core;

,

@Inject(ApiService) api: ApiService;

import { Injectable , Inject } from "@angular/core;
    @Injectable()
    export class apiService {
        public constructor(@Inject(Http)  private http: Http) {}

    }
import { ReflectiveInjector, Component } from '@angular/core';

import { ApiService } from '../api.service';

@Component({
     providers: [ HomeService  ]
})

export class Message {
        timestamp: number;
        message: any;
        api: ApiService;

        constructor(message: any, @Inject(ApiService) api:ApiService) {                                      
                this.timestamp = message.timestamp;
                this.message = message.message;
        }
}
+1

Source: https://habr.com/ru/post/1649322/


All Articles