当前位置:网站首页>uniapp 路由与页面跳转

uniapp 路由与页面跳转

2022-08-10 02:46:00 Little___Turtle

目录

代码演示

运行效果

运行效果

uni.switchTab(OBJECT)

代码演示

运行效果


uni.navigateTo(OBJECT)

保留当前页面,跳转到应用内的某个页面,使用uni.navigateBack可以返回到原页面。

详情官网

OBJECT参数说明

参数类型必填默认值说明平台差异说明
urlString需要跳转的应用内非 tabBar 的页面的路径 , 路径后可以带参数。参数与路径之间使用?分隔,参数键与参数值用=相连,不同参数用&分隔;如 'path?key=value&key2=value2',path为下一个页面的路径,下一个页面的onLoad函数可得到传递的参数

代码演示

index.vue

<template>
	<view class="container">
		<uni-tag text="跳转" @click="toDetails('数据')" type="primary" circle="true" ></uni-tag>
	</view>
</template>

<script setup>
	const toDetails=(data)=>{
		//在起始页面跳转到details.vue页面并传递参数
		uni.navigateTo({
			url: "../details/details?data="+data
		});
	}
</script>

<style lang="scss">
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
	}
</style>

details.vue

<template>
	<view>
		<h2>商品详情</h2>
	</view>
</template>

<script setup>
	import {onLoad} from '@dcloudio/uni-app';
	//接收获取参数
	onLoad((data)=>{
		console.log(data);
	})
</script>

<style>

</style>

运行效果

 

uni.navigateBack(OBJECT)

关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages() 获取当前的页面栈,决定需要返回几层。

详情官网

OBJECT参数说明

参数类型必填默认值说明平台差异说明
deltaNumber1返回的页面数,如果 delta 大于现有页面数,则返回到首页。
animationTypeStringpop-out窗口关闭的动画效果,详见:窗口动画App
animationDurationNumber300窗口关闭动画的持续时间,单位为 msApp

代码演示

<template>
	<view>
		<h2>商品详情</h2>
		<view class="btn">
			<button type="primary" @click="toBack">返回上一页</button>
		</view>
	</view>
</template>

<script setup>
	const toBack = () => {
		uni.navigateBack({
			delta: 1,
			animationType: 'pop-out',
			animationDuration: 200
		});
	}
</script>

<style>

</style>

运行效果

 

uni.switchTab(OBJECT)

跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。

注意: 如果调用了 uni.preloadPage(OBJECT) (opens new window)不会关闭,仅触发生命周期 onHide

OBJECT参数说明

参数类型必填说明
urlString需要跳转的 tabBar 页面的路径(需在 pages.json 的 tabBar 字段定义的页面),路径后不能带参数
successFunction接口调用成功的回调函数
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

代码演示

pages.json

"tabBar": {
	    "list": [{
	      "pagePath": "pages/index/index",
	      "text": "首页"
	    },{
	      "pagePath": "pages/user/user",
	      "text": "用户"
	    }]
	  }

index.vue

<template>
	<view class="container">
		<button type="default" @click="toDetails('数据')">navigateTo跳转tabBar个人中心</button>
		<hr>
		<button type="primary" @click="toUser()">switchTab跳转tabBar个人中心</button>
	</view>
</template>

<script setup>
	const toDetails=(data)=>{
		//在起始页面跳转到user.vue页面并传递参数 不能跳底部导航页面
		uni.navigateTo({
			url: "../user/user?data="+data
		});
	}
	//跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。
	const toUser=()=>{
		uni.switchTab({
			url: '../user/user'
		});
	}
</script>

<style lang="scss">
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
	}
</style>

user.vue

<template>
	<view>
		<h2>用户页面</h2>
	</view>
</template>

运行效果

点击第一个按钮

点击第二个按钮

 

 

原网站

版权声明
本文为[Little___Turtle]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Little___Turtle/article/details/126248683