typescript - Nativescript Tabview 自动更新
问题描述
在 Angular 中使用 Nativescript 5.0
该应用程序正在使用 tabview 结构。每个 tabview 都使用组件来显示数据。
定期更新标签的最佳方法是什么(比如每 30 秒)?
组件调用 api 来获取实时数据。最初,当应用程序启动时,我在各个组件的 ngOnInit() 上调用 api,因此选项卡中填充了数据。
我如何定期调用这些 API,从哪里调用?
这是我的 TabView html 文件的样子:
<TabView id="tabViewContainer">
<StackLayout *tabItem="{title: 'Batches'}">
<StackLayout>
<ns-batches></ns-batches>
</StackLayout>
</StackLayout>
<StackLayout *tabItem="{title: 'Courses'}">
<StackLayout>
<ns-courselist></ns-courselist>
</StackLayout>
</StackLayout>
<StackLayout *tabItem="{title: 'Post'}">
<StackLayout>
<ns-postlist></ns-postlist>
</StackLayout>
</StackLayout>
<StackLayout *tabItem="{title: 'Sales'}">
<StackLayout>
<ns-sales></ns-sales>
</StackLayout>
</StackLayout>
</TabView>
解决方案
在 Tabview 中添加 selectedIndexChanged 事件并更新活动 Tab 的数据
<TabView id="tabViewContainer" (selectedIndexChanged)="onSelectedIndexChanged($event)">
<StackLayout *tabItem="{title: 'Batches'}">
<StackLayout>
<ns-batches (dataYouwanttoUpdate)="updatedInTabViewTs"></ns-batches>
在你的第一个TabItem.ts
import { Input } from '@angular/core';
@Input()
dataYouwanttoUpdate:ObservableArray
在你的 .ts
onSelectedIndexChanged(args: SelectedIndexChangedEventData) {
const tabView = <TabView>args.object;
const selectedTabViewItem = tabView.items[args.newIndex];
switch (args.newIndex) {
case 0:
//update the ns-batches data.
this.updateData()
...
default:
break;
}
}
间隔:数字;
updateData(){
this.interval = setInterval(() => {
this.getdata.getData().subscribe( data => {
this.batches = data.batches ;
});
},500);
}
推荐阅读
- azure-ad-b2c - 使用验证技术配置文件来停止正在运行的函数
- ios - 如何启用 Xcode 调试消息
- swift - 如何在 ImageView 中获取图像的路径
- java - 致命错误编译:无法运行 ecj 编译器:无法识别的选项:--module-version
- gradle - Gradle 外部化配置
- java - 传递方法参考 NoClassDefFoundError
- c++ - 如何用向量将 C++ 函数包装在 C 中
输出参数? - c - 隐式类型转换的意外结果
- python - 如何捕获 Python 中的嵌套函数调用已经捕获的异常
- vba - 如何使用 VBA 将查询结果从子窗体传递到报表?