【发布时间】:2019-12-14 16:11:43
【问题描述】:
我是 Angular 7 中 rxjs 的新手,我有一个返回用户详细信息的 api。我想删除具有相同名称的对象,我几乎没有尝试使用 distinctUntilKeyChanged() 但在控制台中获得的输出与来自 api 响应。我也可以直接使用从服务返回的数据,因为响应已经是使用接口的 Observable,而无需在组件中使用 From() 或 0f()。
这是针对 angular 7 和 Rxjs 6 的,我已经使用 angular 一年了,但直到现在我还没有使用 Rxjs,我在 Rxjs 中遇到了很多困惑,我可以直接在组件中使用来自服务的 Http 响应,否则我是否想使用任何运算符将响应转换为简短的流我可以直接在组件中使用 Json 响应来使用不同的运算符,如 find()、first()、ignoreElements 等...
user.service.ts
private urlLara = 'http://laravel.technalatus.com/public/api/'
public getusers(): Observable<User> {
const head = new HttpHeaders({
'Accept': 'application/json',
'Content-Type': 'application/json',
})
return this.http.get<User>(this.urlLara + 'users', { headers: head
});
}
组件.ts
export class AppComponent implements OnInit {
title = 'angularrxjs';
items: {};
post: Post;
user: User;
public searchTerm: string;
constructor(private UserService: UserService) {
}
ngOnInit() {
this.distinct()
}
public Duchanged() {
// custom compare for name
this.UserService.getusers().pipe(distinctUntilChanged((prev, curr) => prev.name === curr.name))
.subscribe(console.log);
}
获取输出为
[
{id: 2, name: "alshoja", email: "alshoja@gmail.com", email_verified_at: null, type: "admin"},
{id: 3, name: "Ellaware", email: "abhi@gmail.com", email_verified_at: null, type: "user"},
{id: 17, name: "alshoja", email: "c@g.com", email_verified_at: null, type: "user"}
]
期望输出为
[
{id: 2, name: "alshoja", email: "alshoja@gmail.com", email_verified_at: null, type: "admin"},
{id: 3, name: "Ellaware", email: "abhi@gmail.com", email_verified_at: null, type: "user"},
]
【问题讨论】:
标签: angular typescript rxjs angular7 rxjs6