No suitable injection token for parameter 'basePath' of class 'HttpService'

Viewed 30

I am working on a angular project.

I tried multiple inheritance.

@Injectable({
  providedIn: 'root'
})
export class StoreStaffDesignationService extends StoredDataService<IStoreStaffDesignation> {

  constructor(
    public override http: HttpClient,
    ) {
    super('staff-designation', http);
  }
}

@Injectable({
  providedIn: 'root'
})
export class StoredDataService<T> extends HttpService<T> {

  constructor(public subPath: string, public override http: HttpClient) {
    super('store' + '/' + subPath, http);
    this.subPath = subPath;
  }
}

@Injectable({ providedIn: 'root' })
export class HttpService<T> {
    protected baseUrl: string;
    protected basePath: string;
    http: HttpClient;

    constructor(basePath: string, http: HttpClient) {
        this.baseUrl = `${environment.BASE_URL}`;
        this.basePath = basePath;
        this.http = http;
    }
}

I got complie failed with

error NG2003: No suitable injection token for parameter 'subPath' of class 'StoredDataService'. Consider using the @Inject decorator to specify an injection token.

NG2003: No suitable injection token for parameter 'basePath' of class 'HttpService'. Consider using the @Inject decorator to specify an injection token.

What is the mistake in my code?

Any one help me please.

1 Answers

Error mean angular not know how can resolve instance for this variable as it not registered in DI Container or Provider

to solve issue you need to create InjectionToken

export const stringInjector = new InjectionToken<any>('stringInjector');

and use new injector token in contructor

constructor(@Inject(stringInjector) public subPath: string, public override http: HttpClient) {
 }

check link here for more details https://angular.io/errors/NG2003

Related