Làm thế nào để sử dụng observables trong framework Ionic?
Làm thế nào để sử dụng observables trong framework Ionic?
Observable (RxJS) trong Ionic Angular dùng cho HTTP, sự kiện, dữ liệu thời gian thực:
@Injectable({ providedIn: 'root' })
export class UserService {
constructor(private http: HttpClient) {}
list(): Observable<User[]> { return this.http.get<User[]>(`${environment.api}/users`); }
}
// Component: async pipe tự subscribe/ unsubscribe
users$ = this.service.list().pipe(catchError(() => of([])));
// <ion-item *ngFor="let u of users$ | async">{{ u.name }}</ion-item>
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.