angular - Injecting Service to Service -


i built angular application via visual studio template.

following structure given:

  • /clientapp
  • ./app/app.module.shared.ts
  • ./app/app.module.client.ts
  • ./app/app.module.server.ts
  • ./components/*
  • ./services/person-data.service.ts
  • ./services/auth-http.service.ts
  • ./boot-client.ts
  • ./boot-server.ts

so in person-data.service.ts want use auth-http.service.ts.

person-data.service.ts

import { person } '../models/person' import { configuration } '../constants/global.constants'; import { injectable, inject } '@angular/core'; import { http, response, headers } '@angular/http'; import 'rxjs/add/operator/map'; import { observable } 'rxjs/observable'; import { authhttpservice } '../services/auth-http.service';  @injectable() export class personservice {     constructor(private http: http, @inject(authhttpservice)private authhttp: authhttpservice) {          this.actionurl = configuration.api_server + 'api/person/';          this.headers = new headers();         this.headers.append('content-type', 'application/json');         this.headers.append('accept', 'application/json');     }      public getall = (): observable<person[]> => {         return this.authhttp.get(this.actionurl).map((response: response) => <person[]>response.json());     } } 

auth-http.service.ts

import { injectable, inject } '@angular/core'; import { http, response, requestoptions } '@angular/http'; import { observable } 'rxjs/observable'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/catch'; import { authservice } './auth.service';  @injectable() export class authhttpservice {     constructor(private http: http, @inject(authservice) private authservice: authservice) {      }     get(url: string, options?: requestoptions): observable<response> {         console.log("authhttpservice get:" + url);         if (options) {             options = this.authservice._setrequestoptions(options);         } else {             options = this.authservice._setrequestoptions();         }         return this.http.get(url, options);     } } 

app.module.shared.ts

import { ngmodule } '@angular/core'; import { routermodule } '@angular/router'; import { personservice } './services/person-data.service' import { configuration } './constants/global.constants' import { authservice } './services/auth.service' import { authhttpservice } './services/auth-http.service' import { appcomponent } './components/app/app.component'  export const sharedconfig: ngmodule = {     bootstrap: [appcomponent],     declarations: [         appcomponent     ],     providers: [         authhttpservice,         configuration,         personservice,         authservice     ],     imports: [         routermodule.forroot([             { path: '', redirectto: 'home', pathmatch: 'full' },             { path: '**', redirectto: 'home' }         ])     ] }; 

app.module.client.ts

import { ngmodule } '@angular/core'; import { browsermodule } '@angular/platform-browser'; import { formsmodule } '@angular/forms'; import { httpmodule } '@angular/http'; import { browseranimationsmodule } '@angular/platform-browser/animations'; import { sharedconfig } './app.module.shared';   @ngmodule({     bootstrap: sharedconfig.bootstrap,     declarations: sharedconfig.declarations,     imports: [         browsermodule,         formsmodule,         httpmodule,         browseranimationsmodule,         ...sharedconfig.imports     ],     providers: [         { provide: 'origin_url', usevalue: location.origin }     ] }) export class appmodule { } 

when run application following error.

an unhandled exception occurred while processing request. exception: call node module failed error: error: no provider authhttpservice!

what missing?

try remove inject decorators constructor not needed.

then, error says, import inside of providers of module, like:

providers: [              authhttpservice,              // other services here,              { provide: 'origin_url', usevalue: location.origin }             ] 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -