gpt4 book ai didi

ionic3 - ionic3 和 angular 5 中的动态选项卡渲染

转载 作者:行者123 更新时间:2023-12-01 12:13:28 25 4
gpt4 key购买 nike

我想呈现动态选项卡,但我无法执行此操作。第一次选项卡呈现良好,但当我们更改 refreshTabs 函数参数时,它的选项卡显示为首次呈现选项卡。假设第一次加载 APP_TEACHER_TABS 元素,当它更改为 ADMIN 然后它呈现 APP_TEACHER_TABSAPP_ADMIN_TABS 两者,但我想要仅呈现 APP_ADMIN_TABS

root-tabs.ts

import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, PopoverController} from 'ionic-angular';
import {TabInterface} from "../../models/tabsModels";
import {AuthProvider} from "../../providers/auth";
import {APP_ADMIN_TABS, APP_STUDENT_TABS, APP_TEACHER_TABS} from "../../constants";
import {SwitchAccountService} from "../../providers/switch-account";

@IonicPage({
name: 'page-root-tabs',
priority: 'high'
})

@Component({
selector: 'page-root-tabs',
templateUrl: 'root-tabs.html',
})
export class RootTabsPage{

tabs =[];
userLabel: any;

constructor(public navCtrl: NavController,
public navParams: NavParams,
private auth:AuthProvider,
public popoverCtrl: PopoverController,
private switchAccountService: SwitchAccountService) {

this.initializeTabs();

}

initializeTabs(){

this.switchAccountService
.getUserLabel()
.subscribe(message =>{
this.userLabel = message;
this.refreshTabs(this.userLabel);
});
}

refreshTabs(item){

if( item == 'ADMIN'){
while(this.tabs.length){
this.tabs.pop();
}

APP_ADMIN_TABS.forEach(el => {
this.tabs.push(<TabInterface>{label: el.label, icon: el.icon, component: el.component});
});

// this.tabs.splice(this.tabs.indexOf(0), 1);
// setTimeout(()=>{
// APP_ADMIN_TABS.forEach(el => {
// this.tabs.push(<TabInterface>{label: el.label, icon: el.icon, component: el.component});
// });
// },4000);


//this.tabs.splice(0,1);
}

else if(item == 'TEACHER'){

APP_TEACHER_TABS.forEach(el => {
this.tabs.push(<TabInterface>{label: el.label, icon: el.icon, component: el.component});
});

}

else{

if(this.auth.currentUser.user_flag == 2){

APP_TEACHER_TABS.forEach(el => {
this.tabs.push(<TabInterface>{label: el.label, icon: el.icon, component: el.component});
});

}else{

APP_STUDENT_TABS.forEach(el => {
this.tabs.push(<TabInterface>{label: el.label, icon: el.icon, component: el.component});
});

}
}

}

presentPopover(myEvent) {
let popover = this.popoverCtrl.create('page-popover');
popover.present({
ev: myEvent
});
}
}

root-tabs.html

<ion-header>
<ion-navbar no-border-bottom color="customColor">

<ion-title class="custom-font" style="font-size: 2.1em;" text-center>
DASHBOARD
</ion-title>

<ion-buttons end>
<button ion-button icon-only (click)="presentPopover($event)">
<ion-icon name="md-more"></ion-icon>
</button>
</ion-buttons>

</ion-navbar>
</ion-header>

<ion-tabs tabsPlacement="top">
<ion-tab *ngFor="let tab of tabs"
[tabIcon]="tab.icon"
[tabTitle]="tab.label"
[root]="tab.component">
</ion-tab>
</ion-tabs>

最佳答案

我不完全知道你的系统是如何设置的,但是I put together a stackblitz这展示了如何让选项卡随着用户的变化而动态变化。

如您所见,如果您更改其中呈现的实际选项卡,Ionic 并不能真正正常工作。您可以做的是使用 [show] 属性动态显示/隐藏它们。

<ion-tabs #ionTabs>
<ion-tab *ngFor="let tab of tabs$ | async; trackBy: trackByFn"
[tabIcon]="tab.icon"
[tabTitle]="tab.label"
[root]="tab.component"
[show]="tab.show">
</ion-tab>
</ion-tabs>

我将所有选项卡放在一个数组中,并给出了一个可以访问每个选项卡的角色列表。

export const TABS: TabInterface[] = [
{ label: 'Home', icon: 'home', component: HomePage, roles: [UserRole.Admin], show: false },
{ label: 'About', icon: 'information-circle', component: AboutPage, roles: [UserRole.Admin, UserRole.Teacher], show: false },
{ label: 'Contact', icon: 'contacts', component: ContactPage, roles: [UserRole.Teacher, UserRole.Student], show: false }
];

然后随着用户的更改,我处理选项卡以确定用户根据他们拥有的角色可以访问哪些选项卡:

tabs$ = this.userService.user$
.takeUntil(this.destroy$.asObservable())
.map(user => this.processTabsForUser(user))
.do(tabs => this.selectCorrectTab(tabs));

...

private processTabsForUser(user: User): TabInterface[] {
return TABS.map(tab => {
return Object.assign({}, tab, { show: user && user.isAuthorized(tab.roles)});
});
}

如果用户更改而另一个用户已登录,为了正常工作(我不认为这不应该发生在真实系统中,但是......)我必须处理当前选择的选项卡并确保用户可以访问它。如果没有,我会选择用户有权访问的第一个选项卡:

private selectCorrectTab(tabs: TabInterface[]) {
const selectedTab = this.ionTabs.getSelected();
const tab = tabs.find(t => selectedTab && t.component === selectedTab.root);

if (!tab || !tab.show) {
const index = tabs.findIndex(t => t.show);

if (!!~index) {
// bug in Ionic causes the need to do in setTimeout if user changes when already logged in
setTimeout(() => this.ionTabs.select(index));
}
}
}

这应该会得到让选项卡根据登录用户动态更改的最终结果。

关于ionic3 - ionic3 和 angular 5 中的动态选项卡渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50027603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com