当前位置:   article > 正文

【Uni-App】uniapp-H5页面刷新后返回失效,页面栈清空问题,navigateBack失效问题_uni.navigateback()无法返回

uni.navigateback()无法返回

问题描述

在打包生成的H5项目里,刷新后,页面回调,这时候使用uniapp原生的返回api方法会失效。uni.navigateBack()

原因分析:

由于页面回调后导致当前页面刷新,使用getCurrentPages()方法获取页面栈只有当前页面页面无法返回,一直在当前页面刷新闪烁。

解决方案:

想到的解决方案使用原生js的history对象,封装一个兼容uniapp api和原生js的返回的方法。

const navigateBack = (params) => {
	const pages = getCurrentPages()
	if (pages.length === 1) {
		if (typeof params === 'number') {
			history.go(-params)
		} else {
			history.back()
		}
	} else {
		uni.navigateBack()
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

全局封装组件:

<template>
	<view>
		<view class="toBack" >
			<image class="arrow-icon" src="https://image.3rdplanet.cn/upload/banner/icon_fh_16pt%403x.png" @click="navigateBack"></image>
      <view class="title"> {{title}}</view>
      <view class="more"> </view>
		</view>
	</view>
</template>

<script>
	export default{
		props:{
			url:{
				type:String,
				default:()=>{
					return ''
				}
			},
      title:{
        type:String,
				required:false,
        default:''
      }
		},
		methods:{
      /**
        * @description: H5 App通用方案 解决H5刷新返回失败问题
        * @param {*} params
        */
		navigateBack(params) {
	        const pages = getCurrentPages()
	        if (pages.length === 1) {
	          if (typeof params === 'number') {
	            history.go(-params)
	          } else {
	            history.back()
	          }
	        } else {
	          uni.navigateBack()
	        }
     	}
		}
	}
</script>

<style scoped lang="less">
	.toBack{
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    padding: 24rpx 20rpx;
		width: 100%;
		height: 96rpx;
		background-color: #FFFFFF;
		text-align: center;
		font-size: 32rpx;
		font-weight: 400;
		color: #333333;
	  border-top: 1px solid #FFF8F8F8;
    .arrow-icon{
      width: 32rpx;
      height: 32rpx;
      margin: auto 0;
      line-height: 32rpx;
    }
	}
	
</style>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/76950
推荐阅读
  

闽ICP备14008679号