0

I have the following example:

Data model:

export interface SampleList {
    readonly Id: number;
    CompanyName: string;
    Country: string;
}

Component:

export class AppComponent implements OnInit {

  private sampleList = new SampleWrapper<SampleList>();

  constructor() { }

  ngOnInit() {
    this.sampleList.loadData('v_ListOfCompanies');
  }
}

Wrapper class:

export class SampleWrapper<T> {
    public changes: T;
    public original: T;

    private sampleSvc: SampleService;

    constructor() { }

    public loadData(dbView: string) : void {
        this.sampleSvc.getData<T>(dbView)
            .subscribe(
                data => {
                    this.original = data;
                },
                error => {
                    console.log(error);
                }
            );
    }
}

Service:

export class SampleService {

  static readonly apiUrl: string = environment.apiUrl;

  constructor(private http: HttpClient) { }

  getData<T>(dbView: string) : Observable<T> {
    const url = `${SampleService.apiUrl}/${dbView}`;

    return this.http.get<T>(url);
  }
}

The http-Requests fails, because sampleSvc is undefined.

ERROR TypeError: "this.sampleSvc is undefined"

How can I use an ApiService in a wrapper class? Can anyone help me? Or give me some advice about using generic classes in typescript especially Angular 7?

2 Answers 2

1

You are supposed to inject the service using Dependency injection within the constructor

 constructor(private sampleSvc: SampleService) { }

and then use it as,

 this.sampleSvc.getData<T>
Sign up to request clarification or add additional context in comments.

2 Comments

I already tried that. But I get an error error TS2554: Expected 1 arguments, but got 0. I'm not sure to set the SampleService as param in the initialization part.
that means you are passing wrong arguments to the service
0

You need to provide your service inside of constructor

export class SampleWrapper<T> {
    public changes: T;
    public original: T;

    constructor(private sampleSvc: SampleService) { }

    public loadData(dbView: string) : void {
        this.sampleSvc.getData<T>(dbView)
            .subscribe(
                data => {
                    this.original = data;
                },
                error => {
                    console.log(error);
                }
            );
    }
}

And than you should extends your class instead of create new istance of it

export class AppComponent extends SampleWrapper<SampleList> implements OnInit {

  constructor() { }

  ngOnInit() {
    this.loadData('v_ListOfCompanies');
  }
}

But the best way is to use export class SampleWrapper<T> as service if that component does not have any view.

3 Comments

I'm getting an error when I initialized the Wrapper class => error TS2554: Expected 1 arguments, but got 0.
But I have more Interfaces.. Lookups for dropdowns. I can't set all these lookups in the extends-Statement.
I have asked if SampleWrapper component has a view if not, it for sure should be a service then you do not have to extend.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.