uniapp development applet Return to the previous page

The applet navigation bar uses the uniapp component. The attribute parameters in the navigation go to the official website to view the uni-nav-bar component

@clickLeft Triggered when the left button is clicked
 <uni-nav-bar class="nav-bar-top" :fixed="true" leftIcon="arrowleft" leftText="课程报名" @clickLeft="back()"
      color="#ffffff" :border="false" :status-bar="true" />

 Trigger the back() method to handle the return page

getCurrentPages() The function is used to obtain the instance of the current page stack, which is given in the order of the stack in the form of an array, the first element is the home page, and the last element is the current page. The array length minus one is the current page, and the array length minus two is the previous page

page.route Get the route of the current page

	back() {
			let pages = getCurrentPages();   // 获取当前页面栈的实例
			let currPage = pages[pages.length - 1];   //当前页面
			let prevPage = pages[pages.length - 2];  //上一个页面

            //判断上一页是否为首页,如果是就直接返回首页
			if(prevPage && prevPage.route && prevPage.route.indexOf("/jump") != -1){
				uni.switchTab({
					url:'/tabBar/home/index'  //路径为测试数据,填写小程序真实路径就行
				});
				return;
			}

            //uni.navigateBack() 关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages() 获取当前的页面栈,决定需要返回几层
			uni.navigateBack({
				success:() => {
                    delta: 1 //返回的页面数,如果 delta 大于现有页面数,则返回到首页。默认为1
					//console.log('success')
				},

                //失败回调直接返回首页
				fail:() => {
					//console.log('fail')
					uni.switchTab({
						url:'/tabBar/home/index'  //路径为测试数据,填写小程序真实路径就行
					})
				}
			})
		},

Guess you like

Origin blog.csdn.net/weixin_64103049/article/details/128493025