tabbar-badge.js 754 B

123456789101112131415161718192021222324252627282930313233343536
  1. import {mapState} from 'vuex'
  2. export default {
  3. computed: {
  4. ...mapState('m_user', ['userinfo'])
  5. },
  6. watch: {
  7. // 监听 count 值的变化
  8. 'userinfo.count': {
  9. handler() {
  10. // 调用 methods 中的 setBadge 方法,重新为 tabBar 的数字徽章赋值
  11. this.setBadge()
  12. }
  13. },
  14. },
  15. onShow() {
  16. // 在页面刚展示的时候,设置数字徽标
  17. this.setBadge()
  18. },
  19. methods: {
  20. setBadge() {
  21. if (this.userinfo.count == 0) {
  22. uni.removeTabBarBadge({
  23. index: 2
  24. })
  25. } else {
  26. // 调用 uni.setTabBarBadge() 方法,为购物车设置右上角的徽标
  27. uni.setTabBarBadge({
  28. index: 2,
  29. text: this.userinfo.count + '', // 注意:text 的值必须是字符串,不能是数字
  30. })
  31. }
  32. }
  33. }
  34. }