解决uniapp不显示showToast或一闪而过问题

发布时间 2023-04-21 10:41:25作者: 东方昭月

原因:showToast与其他方法冲突,以onLoad冲突为例

第一种:

将showToast与showLoading分开,放到mounted中

mounted(){
  // 判断是否登录
  if (!this.hasLogin) {
    uni.showToast({
    title:'登录过期',
    icon:'none'
    })
  }
},
onLoad(option){
  // 获取字典
  this.$store.dispatch('getDict', 'sex').then(res => {
  this.dicts = res
  })
},

第二种(亲测有效):

使用延时

onLoad(option){
    // 判断是否登录
    if (!this.hasLogin) {
      setTimeout(()=>{
      uni.showToast({
      title:'登录过期(通过延时)',
      icon:'none'
      })
      },10)
    }
// 获取字典
this.$store.dispatch('getDict', 'sex').then(res => {
  this.dicts = res
  })
},
再次测试也可以成功显示

以上两种方法都有一个弊端,就是showToast的时候无法showLoading,反之亦然showLoading无法showToast,如果大家有什么好办法欢迎留言交流。
参考链接:https://blog.csdn.net/QINGZHID/article/details/126601064