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

Bia*_*meê 3 angular ionic4

我有一个ion-tab与型动物的方式来重定向:通过在路线tabs.module.ts,并通过[routerLink]在HTML中。

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

使用 CSS:

ion-tab-button {
 --color-selected: var(--ion-color-text-secondary);
}
Run Code Online (Sandbox Code Playgroud)

我尝试loadChildren在所有选项卡中使用路由,但有些选项卡要重定向到组件并且没有成功。我还尝试在 CSS 中使用伪类,例如

ion-tab-button:active {
    color: blue;
}
Run Code Online (Sandbox Code Playgroud)

实际文件:

标签页.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>
Run Code Online (Sandbox Code Playgroud)

tabs.module.ts

const routes: Routes = [
  {
    path: '',
    component: TabsPage,
    children: [
     {
        path: '',
        redirectTo: '/tabs/home',
        pathMatch: 'full'
     },
      {
        path: 'home',
        loadChildren: '../home/home.module#HomePageModule'
      },
    ]
  }
];
Run Code Online (Sandbox Code Playgroud)

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

Ric*_*lis 8

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

您可以订阅 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;
      }
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

标签页.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>
Run Code Online (Sandbox Code Playgroud)

tabs.page.scss

.active-item {
  color: red;
}
Run Code Online (Sandbox Code Playgroud)

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

我在这里遇到了 Simon Grimm 的这种模式。他有一些非常棒的教程。

希望这可以帮助