【发布时间】:2020-06-06 16:33:04
【问题描述】:
我有一个使用rest api的角度服务。
@Injectable({
providedIn: 'root'
})
export class ProductService {
private url: string = "/app/products";
constructor(private http: HttpClient) {
}
get(caregoryId: string) {
return this.http.get<Product[]>(`${this.url}`)
.pipe(map(products => products.filter(p => p.caregoryId == caregoryId)))
}
add(p: Product) {
return this.http.post<Product>(this.url, p);
}
}
我的组件是:
export class ProductComponent implements OnInit{
items: Observable<Array<Product>>;
ngOnInit() {
this.route.params.subscribe(
(params: any) => {
this.items = this.productService.get(params.id);
})
}
addWidget() {
let item: any = { name: "p-1", color: "red" } }
this.productService.add(item))
/// how to add created item in my items array ?
}
}
我可以获取产品列表并使用 html 中的异步管道列出它们。但我无法在我的可观察数组中添加创建的项目。我该怎么做?
【问题讨论】:
-
您在
this.productService.add(item)中缺少subscribe。 -
@developer033 OP 正在恢复 observable 所以他们可能会在其他地方订阅它
-
@barteloma 请你看看我的回答对你有没有帮助?
标签: angular rxjs observable