【发布时间】:2020-03-28 05:46:35
【问题描述】:
我有两个组件和一个服务。
在组件 1 中,我有一个在初始化时立即调用的函数。
组件 1:
ngOnInit() {
this.getItems();
}
getItems(){
this.mainService.getItems().subscribe(items => { //WATCH SERVICE
this.items = items;
});
}
现在,在服务中,我拥有正确完成工作的函数 getItems():
主要服务:
public itemCategoryId = new BehaviorSubject<any>(1); //watch category
getItems(): Observable<Item[]> { //THE MAIN FUNCTION
return this.http.get<Item[]>(Constants.endPoint + Constants.allPins + this.itemCategoryId.value + '/1');
}
changeCategoryId(id) {
this.itemCategoryId.next(id);
}
现在,在组件 2 中,我通过一个新号码激活服务中的功能 changeCategoryId。 这会改变 itemCategoryId BehaviorSubject。
组件 2
changeCategory(id) {
console.log("ADMIN Category is now: ", id);
this.mainService.changeCategoryId(id);
}
所以现在,在组件2中点击后,服务中的函数getItems()有不同的http get请求。这个新请求应该在组件 1 中加载。但它没有。
我的问题是,为什么组件 1 没有从服务中的 getItems() 函数获取新项目,即使我订阅了它?
我应该再调用一次组件 1 中的 getItems() 函数还是我缺少其他解决方案?
【问题讨论】:
标签: angular