在Ionic 3中的图标上添加徽章

Osh*_*rth 7 css sass ionic-framework ionic2 ionic3

我需要在离子3中的购物车图标上添加一个数字徽章,以便用户获得购物车中元素数量的更新而不实际访问页面我试图一起使用按钮和徽章的代码但是它没有用

  <button ion-button icon-only (click)="cart()">
      <ion-icon name="cart"> <ion-badge item-end>260k</ion-badge></ion-icon>
  </button>
Run Code Online (Sandbox Code Playgroud)

上面的代码显示了购物车图标旁边的徽章,但没有显示在它上面,所以有没有办法像我们在通知警报徽章中那样为图标本身添加徽章?

Jac*_*ack 10

我认为你必须使用一些CSS和绝对定位将徽章放在购物车图标的左上角.

像这样的东西:

<button id="cart-btn" ion-button icon-only (click)="cart()">
      <ion-icon name="cart"></ion-icon>
      <ion-badge id="cart-badge">260k</ion-badge>
</button>
Run Code Online (Sandbox Code Playgroud)

CSS

#cart-btn {
   position: relative;
}

#cart-badge {
   position: absolute;
   top: 0px;
   right: 0px;
}
Run Code Online (Sandbox Code Playgroud)

  • 你的答案中有一个错字.它应该是位置:绝对的. (3认同)