(IONIC 4)离子选项卡按钮在单击/选择时不会更改颜色

时间:2019-07-12 17:38:48

标签: angular ionic4

我有一个ion-tab,具有不同的重定向方式:tabs.module.ts处的路由和html中的[routerLink]处。

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

使用CSS:

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

我尝试在所有选项卡中使用loadChildren路由,但是某些选项卡将重定向到组件,但未成功。 我还尝试在

之类的CSS中使用伪类。
ion-tab-button:active {
    color: blue;
}

实际文件:

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]">
      <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'
      },
    ]
  }
];

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

2 个答案:

答案 0 :(得分:0)

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

您可以在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。

我遇到了西蒙·格里姆(Simon Grimm)的这种模式here。他有一些很棒的教程。

希望这会有所帮助

答案 1 :(得分:0)

当您检测到正确的路径时,可以将“ tab-selected”按钮的类别从true更改为false。

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