I am not build a service from angular lib, getting error. here is my code and error. any help please? I am using angular 8
import { Injectable } from '@angular/core';
// @dynamic
@Injectable({
providedIn: 'root'
})
export class SharedSortByColumnService {
constructor(
private data: any,
private searchCriteria: any,
private childName: string,
private secondChildName: string,
private thirdChildname: string) { }
columnSorter() {
}
}
error:
Can't resolve all parameters for SharedSortByColumnService in C:/722333/xxx/services/shared-sort-by-column.service.ts: (?, ?, ?, ?, ?).
UPDATE
I tried this:
constructor(
public data?: any,
public searchCriteria?: any,
public childName?: string,
public secondChildName?: string,
public thirdChildname?: string) { }
still not work.
You can't magically resolve a whole bunch of strings and "any" data types using DI.
Angular is good at resolving classes that are known to be injectable - but how is it meant to differentiate between the different string params here?
Where are those values coming from? If they are config values that remain unchanged for the lifetime of the app, then you could set them in the constructor (not by attempted DI):
@Injectable({
providedIn: 'root'
})
export class SharedSortByColumnService {
constructor() {
// TODO: set config values here
// this.data = ??
}
private data: any;
private searchCriteria: any;
private childName: string;
private secondChildName: string;
private thirdChildname: string;
columnSorter() {
}
}
Edit:
I think you're implying that these are actually function parameters.
In which case it's trivial to write a function that acceps these parameters:
@Injectable({
providedIn: 'root'
})
export class SharedSortByColumnService {
constructor() {
// empty constructor, can be deleted
}
columnSorter(data: any, searchCriteria: any, childName: string,
secondChildName: string, thirdChildname: string): data {
// TODO: process data.
// No need to maintain internal state in the service.
// All parameters relate to this single transaction / operation
return data;
}
}
The service should only maintain internal state where it makes sense to maintain it over the lifetime of the app.
If you want to sort two data sets in the same component, for example, then you call the service twice and are "transactional" actions - the function calls are just a case of data + criteria in, and data out.