我不确定这里的小细节,所以也许这甚至不应该工作 - 但似乎应该有一些解决方案。我有一个在选项卡中使用两个子组件的父组件,如下所示:
<div class="container" *ngIf="page === 'tab1'">
<refresher-component></refresher-component>
<child-component1></child-component1>
</div>
<div class="container" *ngIf="page === 'tab2'">
<refresher-component></refresher-component>
<child-component2></child-component2>
</div>
父级还有一个刷新组件,负责刷新两个选项卡中的数据。每个子组件都注入一个实现 Refreshable 接口的服务:
- 子组件 1:
constructor(private service1: Service1)
- 子组件 2:
constructor(private service2: Service2)
每个服务看起来像这样(将 1 替换为 2 用于第二个服务):
@Injectable()
export class Service1 implements Refreshable<Type1[]>
refersher-component
看起来像这样:
public constructor(@Inject(RefresherServiceToken) public refreshableService: Refreshable<any>) {
console.log('refreshable', this.refreshableService);
}
发生的情况是,当我加载页面时(默认为第一个选项卡),console.log
输出 Service1. 但是,当切换到 Tab 2 时,再次调用了构造函数,但它也输出了 Service1。因此刷新也是对Service1的数据进行的,不影响Service2。
这是正常行为吗,因为路线没有改变,我需要添加一些东西来重置服务吗?一般来说,有没有一种简单的方法来解决这种行为?(角度 8)