gpt4 book ai didi

angular - (IONIC 4) ion-tab-button 在点击/选择时不会改变颜色

转载 作者:行者123 更新时间:2023-12-04 01:05:34 24 4
gpt4 key购买 nike

我有一个 ion-tab使用不同的重定向方式:通过 tabs.module.ts 的路由来自 [routerLink]在 html 中。

问题是:当ion-tab-buttons单击后,它们会正确导航到其他页面,但颜色仅在使用 Routes 和“loadchildren”路由时才会更改。

使用 CSS:

ion-tab-button {
--color-selected: var(--ion-color-text-secondary);
}

我尝试使用 loadChildren在所有选项卡中路由,但有些选项卡要重定向到组件并且未成功。
我还尝试在 CSS 中使用伪类,例如
ion-tab-button:active {
color: blue;
}

实际文件:

标签页.html
<ion-tabs>
<ion-tab-bar slot="bottom">
<ion-tab-button tab="home">
<i class="fal fa-home-alt fa-2x"></i>
</ion-tab-button>

<ion-tab-button [routerLink]="['/tabs/home/feed/feed-user/' + id]">
<i class="fal fa-user-circle fa-2x"></i>
</ion-tab-button>

<ion-tab-button [routerLink]="['/tabs/home/trade-room']">
<i class="fal fa-comments-alt-dollar fa-2x"></i>
</ion-tab-button>
</ion-tab-bar>
</ion-tabs>

tabs.module.ts
const routes: Routes = [
{
path: '',
component: TabsPage,
children: [
{
path: '',
redirectTo: '/tabs/home',
pathMatch: 'full'
},
{
path: 'home',
loadChildren: '../home/home.module#HomePageModule'
},
]
}
];

我需要一些方法来在单击它们时更改颜色按钮

最佳答案

我不确定这正是您要寻找的,但也许您可以使用某种形式的这种想法来解决您的问题。

您可以订阅 tabs.page.ts 中的路由器事件并评估 url 并在 url 与重定向之一匹配时应用 css。

tabs.page.ts

import { Component } from '@angular/core';
import { Router, RouterEvent } from '@angular/router';

@Component({
selector: 'app-tabs',
templateUrl: 'tabs.page.html',
styleUrls: ['tabs.page.scss']
})
export class TabsPage {

selectedPath = '';

constructor(private router:Router) {
this.router.events.subscribe((event: RouterEvent) => {
if(event && event.url){
this.selectedPath = event.url;
}
});
}
}

tabs.page.html
<ion-tabs>
<ion-tab-bar slot="bottom">
<ion-tab-button tab="home">
<i class="fal fa-home-alt fa-2x"></i>
</ion-tab-button>

<ion-tab-button [routerLink]="['/tabs/home/feed/feed-user/' + id]" [class.active-item]="selectedPath.includes('feed')">
<i class="fal fa-user-circle fa-2x"></i>
</ion-tab-button>

<ion-tab-button [routerLink]="['/tabs/home/trade-room']" [class.active-item]="selectedPath.includes('trade')">
<i class="fal fa-comments-alt-dollar fa-2x"></i>
</ion-tab-button>
</ion-tab-bar>
</ion-tabs>

tabs.page.scss
.active-item {
color: red;
}

这不会影响 tabs.module.ts 中定义的选项卡的现有行为。您可能需要编写更多内容来管理这些选项卡的 css。

我遇到了这个模式 here通过西蒙格林。他有一些非常棒的教程。

希望这可以帮助

关于angular - (IONIC 4) ion-tab-button 在点击/选择时不会改变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57011707/

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