Javascript 离子2:在选项卡按钮中使用图片

Javascript 离子2:在选项卡按钮中使用图片,javascript,angular,tabs,ionic2,Javascript,Angular,Tabs,Ionic2,我在我的应用程序中使用离子标签,我想在标签按钮中使用图片。 我想动态地设置这张图片 在我的例子中,我有一个与不同用户链接的帐户。我想根据所选用户更改选项卡图片 我有这个: 我想要这个: 我的选项卡中的代码: <ion-tabs tabsHighlight="false"> <ion-tab [root]="HomePage" tabsHideOnSubPages="true" tabIcon=

我在我的应用程序中使用离子标签,我想在标签按钮中使用图片。 我想动态地设置这张图片

在我的例子中,我有一个与不同用户链接的帐户。我想根据所选用户更改选项卡图片

我有这个:

我想要这个:

我的选项卡中的代码:

    <ion-tabs tabsHighlight="false">
      <ion-tab [root]="HomePage" 
               tabsHideOnSubPages="true"
               tabIcon="checkbox"
               tabTitle="A faire"
               tabBadge="5"
               tabBadgeStyle="notif">
      </ion-tab>
      <ion-tab [root]="ToComePage"
               tabsHideOnSubPages="true"
               tabIcon="time" tabTitle="A venir"
               tabBadge="0"
               tabBadgeStyle="notif">
      </ion-tab>
      <ion-tab [root]="HistoricPage"
               tabsHideOnSubPages="true"
               tabIcon="book"
               tabTitle="Historique">
      </ion-tab>
      <ion-tab [root]="MenuPage"
               tabsHideOnSubPages="true"
//I want to delete this tab Icon and replace it by a picture.
               tabIcon="menu"
               tabTitle="Menu">
      </ion-tab>
    </ion-tabs>


我不知道怎么做,好主意吗?

选项卡图标
自定义名称

<ion-tab [root]="MenuPage"
       tabsHideOnSubPages="true"
       tabIcon="customicon"
       tabTitle="Menu">
</ion-tab>

我终于找到了解决办法! 我只是在创建的DOM中编写

我喜欢这样:

updateAccountTab() : void {
      let array = document.getElementsByClassName('tabbar');
      let tabbar = array[0];
      let element = tabbar.childNodes[tabbar.childElementCount-1];
      if(element) {
          element.removeChild(element.childNodes[1]);
          let img = document.createElement("img");
          img.setAttribute("class", "tab-icon-custom tab-button-icon icon icon-md");
          img.setAttribute("src", this.pdata.user.profile_thumbnail);
          element.insertBefore(img, element.childNodes[1]);
      }
  }
在html中:

<ion-tabs tabsHighlight="false" #myTab>
      <ion-tab [root]="HomePage" 
               tabsHideOnSubPages="true"
               tabIcon="checkbox"
               tabTitle="A faire"
               tabBadge="5"
               tabBadgeStyle="notif">
      </ion-tab>
      <ion-tab [root]="ToComePage"
               tabsHideOnSubPages="true"
               tabIcon="time" tabTitle="A venir"
               tabBadge="0"
               tabBadgeStyle="notif">
      </ion-tab>
      <ion-tab [root]="HistoricPage"
               tabsHideOnSubPages="true"
               tabIcon="book"
               tabTitle="Historique">
      </ion-tab>
      <ion-tab [root]="MenuPage"
               tabsHideOnSubPages="true"
//I want to delete this tab Icon and replace it by a picture.
               tabIcon="menu"
               tabTitle="Menu">
      </ion-tab>
</ion-tabs>
在您的scss文件中:

.tab-icon-custom {
    height: 100%;
    object-fit: cover;
    margin: 0 auto;
    width: 50px;
    border-radius: 50%;
 }

请发布一些代码来演示您的尝试。我更改了我的帖子。我不知道你需要什么才能更好地理解我想做什么?谢谢,这很有效!但是我怎样才能改变图片的大小呢?我试图修改宽度,但没有效果。我如何动态更改图片URL?你知道如何更改图片URL吗?有人这样做了吗?如何更改图片url(动态)如果你愿意,我找到了一个解决方案。这不是在Angular/Ionic应用程序中与DOM交互的推荐方法。这可能会导致一些性能问题。当然,我理解。但我没有找到另一种方法来做我想做的…哦,好吧。今天晚些时候,我将编辑您的答案,并包括与此完全相同的离子方式:)哦,我期待着!:D它将是完美的^^@塞巴费雷拉斯你找到了一种“离子方式”吗?
import { ViewChild } from '@angular/core';
import {Tabs} from 'ionic-angular';
export class TabsPage {

  @ViewChild('myTab') tabRef: Tabs;
  constructor(public navCtrl: NavController) {

  }
  ngAfterViewInit() {
    let tabbar = this.tabRef._tabbar.nativeElement;
    let element = tabbar.childNodes[tabbar.childElementCount-1];
    if(element) {
      element.removeChild(element.childNodes[1]);
      let img = document.createElement("img");
      img.setAttribute("class", "tab-icon-custom");
      img.setAttribute("src", 'https://picsum.photos/200');
      element.insertBefore(img, element.childNodes[1]);
    }

  }
}
.tab-icon-custom {
    height: 100%;
    object-fit: cover;
    margin: 0 auto;
    width: 50px;
    border-radius: 50%;
 }