skip to content
Alvin Lucillo

Injectable service

/ 1 min read

It’s easy to make a class, especially made as a service, to be injectable. The test service in the example below can be used in any components via inject.

test.service.ts

@Injectable({
	providedIn: "root",
})
export class TestService {
	#messageSignal = signal("");
	message = this.#messageSignal.asReadonly();

	showMessage(text: string) {
		this.#messageSignal.set(text);
	}
}
// some component
testService = inject(TestService);
// ...
this.testService.showMessage("hello");