Dependency Injection
Creating Services
import { resolve } from '@aurelia/kernel';
import { IHttpClient } from '@aurelia/fetch-client';
export class UserService {
private readonly http = resolve(IHttpClient);
private readonly cache: User[] = [];
async getUsers(): Promise<User[]> {
const response = await this.http.fetch('/api/users');
const payload = await response.json();
this.cache.splice(0, this.cache.length, ...payload);
return this.cache;
}
async createUser(userData: CreateUserRequest): Promise<User> {
const response = await this.http.fetch('/api/users', {
method: 'POST',
body: JSON.stringify(userData),
headers: { 'Content-Type': 'application/json' },
});
const user = await response.json();
this.cache.push(user);
return user;
}
}Service Registration
Using Services in Components
Service Dependencies
Service Lifetimes
Configuration Services
Resolver toolbox
Resolver
Example
What it does
Creating custom resolvers
Testing with DI
What's Next
Last updated
Was this helpful?