Hello !
I'm refactoring my code to fully embrace signals and try to follow the best practices.
But I would like to be sure it's really the best practice (pretty sure it is ... to be honest the code seems so much nicer now).
And I'm also struggling testing my component and I'm not sure the solution I've found is the correct one (or at least I would be glad to have some other proposal).
Here is an example with a dummy component.
I went from class component with observable to signal based component
// OLD
@Component({
...
})
export class StuffComponent implements OnInit {
private readonly _stuffService: StuffService = inject(StuffService);
public stuffList: Stuff[] = [];
public ngOnInit(): void {
this._stuffService.getStuffList().subscribe((stuffList) => {
this.stuffList = stuffList;
})
}
}
to
@Component({
...
})
export class StuffComponent {
private readonly _stuffService: StuffService = inject(StuffService);
public readonly stuffList: Signal<Stuff[]> = toSignal(this._stuffService.getStuffList());
}