3

I'm trying to create a small framework to make it easier for me to implement CRUD functionality.

Therefore I've created a BaseHttpService like this:

... <imports> ...
@Injectable()
export class BaseHttpService {
  constructor(
    private http: Http
  ) {
    console.log('ctor');
    this.apiUrl$.subscribe(apiUrl => this.rootApiUrl = apiUrl.removeTrailingSlash());
  }
  public getPagedResultOfType<T extends IBaseEntity>(url: string, vm: ISearchVm<T>, options?: RequestOptions): Observable<PagedResult<T>>{ ... <implementation> ... }
  ... <other methods> ...
}

Then I have my BaseEntityReadOnlyService that uses this BaseHttpService to create the Read fucntionality of the CRUD:

@Injectable()
export class BaseAppSumEntityReadOnlyService<T extends IBaseEntity>{
  constructor(
    @Inject(forwardRef(() => BaseHttpService)) public baseHttpService: BaseHttpService
    // public baseHttpService: BaseHttpService
  ) { 
    console.log('ro ctor');
    console.log(this.baseHttpService); // <-- this is always undefined
  }

  getAll(vm: ISearchVm<T>): Observable<PagedResult<T>>{
    const url = `${this.baseUrl}`;
    console.log(this.baseHttpService);
    return this.baseHttpService.getPagedResultOfType<T>(url, vm); // <-- this fails because baseHttpService is undefined: Cannot read property 'getPagedResultOfType' of undefined
  }
  ... <more methods>
}

In my FrameworkModule, I've tried alot:

@NgModule({
  declarations: [
    ...
  ],
  imports: [
    CommonModule,
    HttpModule
  ],
  providers: [
    BaseHttpService,
    // BaseAppSumEntityReadOnlyService
    { provide: BaseAppSumEntityReadOnlyService, useClass: BaseAppSumEntityReadOnlyService, deps: [ BaseHttpService ] },
  ]
})
export class AppSumFrameworkModule { }

This is what I currently have, but this does not work. The error I get is:

Cannot read property 'getPagedResultOfType' of undefined

How can I get the BaseHttpService to be injected in my BaseAppSumEntityReadOnlyService?

0

1 Answer 1

1

Import your service like this:

import { BaseHttpService} from '../servicePath';

Then declare it like this in your constructor:

constructor(
    private baseHttpService: BaseHttpService
  ) { 
    console.log(this.baseHttpService); 
  }

Let me know if it works like this, and if not, copy/paste the console error please. :D

Edit 1:

Service: BaseHttpService

... <imports> ...
@Injectable()
export class BaseHttpService {
  constructor(
    private http: Http
  ) {
    console.log('ctor');
    this.apiUrl$.subscribe(apiUrl => this.rootApiUrl = apiUrl.removeTrailingSlash());
  }
  public getPagedResultOfType<T extends IBaseEntity>(url: string, vm: ISearchVm<T>, options?: RequestOptions): Observable<PagedResult<T>>{ ... <implementation> ... }
  ... <other methods> ...
}

Service: BaseAppSumEntityReadOnlyService

@Injectable()
export class BaseAppSumEntityReadOnlyService<T extends IBaseEntity>{
  constructor(
    private baseHttpService: BaseHttpService
  ) { 
    console.log('ro ctor');
    console.log(this.baseHttpService); // <-- this is always undefined
  }

  getAll(vm: ISearchVm<T>): Observable<PagedResult<T>>{
    const url = `${this.baseUrl}`;
    console.log(this.baseHttpService);
    return this.baseHttpService.getPagedResultOfType<T>(url, vm); // <-- this fails because baseHttpService is undefined: Cannot read property 'getPagedResultOfType' of undefined
  }
  ... <more methods>
}

Module:

@NgModule({
  declarations: [
    myComponent
  ],
  imports: [
    CommonModule,
    HttpModule
  ],
  providers: [
    BaseHttpService,
    BaseAppSumEntityReadOnlyService,
  ]
})
export class AppSumFrameworkModule { }

Component: myComponent

... <imports> ...
@Component()
export class myComponent {
 constructor(
    private baseAppService: BaseAppSumEntityReadOnlyService
  ) { 
    console.log(this.baseAppService); 
  }
}
Sign up to request clarification or add additional context in comments.

10 Comments

This is how it is currently done: import { BaseHttpService } from './base-http.service'; @Injectable() export class BaseAppSumEntityReadOnlyService<T extends IBaseEntity>{ constructor( protected baseHttpService: BaseHttpService ) { console.log('ro ctor - '); console.log(this.baseHttpService); } } // The console logs give: 'ro ctor -' undefined
Change for a second there protected for private and recompile the App. See if the issue persists. I will keep looking into your issue and see if I find where the problem is
@AppSum In you example code I see this @Inject(forwardRef(() => BaseHttpService)) public baseHttpService: BaseHttpService. Which version is the one that you are using then? The one on your OP or the one on this comment?
Changing it to private gives the same result. (just so you know why, I'm setting it as protected to be able to use it in the service that will implement the rest of the CRUD functions and will extend this BaseReadOnlyService)
I'm using the Angular CLI, so it's pretty easy to change the code and test different stuff. I've tried both the forwardRef and the private baseHttpService: BaseHttpService. They result in the same.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.