动态生成Tabs和TabContent(数量和内容由后端数据决定),核心是通过数据驱动UI,利用循环渲染(ForEach)结合后端返回的数据源实现。以下是具体实现步骤:
// 定义单个Tab的数据结构 interface TabItem { id: string; // 唯一标识 title: string; // Tab标题 content: string; // Tab对应的内容(可根据实际需求扩展) }
// 表示添加或删减子页面的状态,true为添加,false为删减 updateState: boolean = true; // @watch对count进行监听,当count发生变化,执行updateTabList() @State @Watch('updateTabList') count: number = 0; // 模拟后端返回的数据(实际中通过http请求获取) @State tabList: TabItem[] = [ { id: '1', title: '推荐', content: '推荐内容' }, { id: '2', title: '热点', content: '热点内容' }, ];
// 模拟后端数据更新(实际中在http请求回调中执行) updateTabList() { if (this.updateState) { // 添加子页面的操作 this.tabList = [ ...this.tabList, // 保留原有数据 { id: `${this.count + 2}`, title: `新增Tab${this.count}`, content: `新增内容${this.count}` }// 新增数据 ]; } else { // 删减子页面的操作 this.tabList.pop(); } };
Column() { Tabs() { // 循环生成TabContent,标题由每个item的title决定 ForEach(this.tabList, (item: TabItem) => { TabContent() { // 每个Tab的内容,可替换为复杂组件 Column({ space: 10 }) { Text(item.content) .width('100%') .layoutWeight(1) .textAlign(TextAlign.Center); // 在推荐页面添加两个按钮用于添加或删除子页面 if (item.id === '1') { Button('添加子页面') .width('80%') .height(50) .borderRadius(20) .margin({ bottom: 16 }) .onClick(() => { // 先对updateState赋值 this.updateState = true; // 再对count进行操作 this.count += 1; }); Button('删除子页面') .width('80%') .height(50) .borderRadius(20) .margin({ bottom: 16 }) .onClick(() => { this.updateState = false; this.count -= 1; }); } } .width('100%') .height('100%'); } .tabBar(item.title); // 设置当前Tab的标题 }, (item: TabItem) => item.id); // 唯一键(必填,用于DiffUI) } .width('100%') .height('100%'); };
完整示例参考如下:
// 定义单个Tab的数据结构 interface TabItem { id: string; // 唯一标识 title: string; // Tab标题 content: string; // Tab对应的内容(可根据实际需求扩展) } @Entry @Component struct DynamicTabsPage { // 表示添加或删减子页面的状态,true为添加,false为删减 updateState: boolean = true; // @watch对count进行监听,当count发生变化,执行updateTabList() @State @Watch('updateTabList') count: number = 0; // 模拟后端返回的数据(实际中通过http请求获取) @State tabList: TabItem[] = [ { id: '1', title: '推荐', content: '推荐内容' }, { id: '2', title: '热点', content: '热点内容' }, ]; // 模拟后端数据更新(实际中在http请求回调中执行) updateTabList() { if (this.updateState) { // 添加子页面的操作 this.tabList = [ ...this.tabList, // 保留原有数据 { id: `${this.count + 2}`, title: `新增Tab${this.count}`, content: `新增内容${this.count}` } // 新增数据 ]; } else { // 删减子页面的操作 this.tabList.pop(); } }; build() { Column() { Tabs() { // 循环生成TabContent,标题由每个item的title决定 ForEach(this.tabList, (item: TabItem) => { TabContent() { // 每个Tab的内容,可替换为复杂组件 Column({ space: 10 }) { Text(item.content) .width('100%') .layoutWeight(1) .textAlign(TextAlign.Center); // 在推荐页面添加两个按钮用于添加或删除子页面 if (item.id === '1') { Button('添加子页面') .width('80%') .height(50) .borderRadius(20) .margin({ bottom: 16 }) .onClick(() => { // 先对updateState赋值 this.updateState = true; // 再对count进行操作 this.count += 1; }); Button('删除子页面') .width('80%') .height(50) .borderRadius(20) .margin({ bottom: 16 }) .onClick(() => { this.updateState = false; this.count -= 1; }); } } .width('100%') .height('100%'); } .tabBar(item.title); // 设置当前Tab的标题 }, (item: TabItem) => item.id); // 唯一键(必填,用于DiffUI) } .width('100%') .height('100%'); }; } }