创建page1和page2

确保在pages.json文件中的pages属性下有这两个页面的索引

	"pages": [ 
		{
			"path" : "pages/page1/page1",
			"style" : 
			{
				"navigationBarTitleText" : ""
			}
		},
		{
			"path" : "pages/page2/page2",
			"style" : 
			{
				"navigationBarTitleText" : ""
			}
		}
	],

1.组件跳转实现page1跳转page2

该组件类似HTML中的<a>组件,但只能跳转本地页面,url是要跳转的页面索引,在pages.json里有对应页面的索引

page1代码

<template>
	<div>
		<!-- 组件跳转 -->
		<h1>我是page1页面</h1>
		<navigator url="/pages/page2/page2" hover-class="navigator-hover">
			<button type="default">跳转到新页面</button>
		</navigator>
	</div>
</template>

<script setup></script>

<style></style>

page2代码

<template>
 <div>
	 <h1>我是page2页面</h1>
 </div>
</template>

<script setup></script>

<style></style>

实现效果

                  

下面是navigator的属性

属性名类型默认值说明平台差异说明
urlString应用内的跳转链接,值为相对路径或绝对路径,如:"../first/first","/pages/first/first",注意不能加 .vue 后缀
open-typeStringnavigate跳转方式
deltaNumber当 open-type 为 'navigateBack' 时有效,表示回退的层数
animation-typeStringpop-in/out当 open-type 为 navigate、navigateBack 时有效,窗口的显示/关闭动画效果,详见:窗口动画App
animation-durationNumber300当 open-type 为 navigate、navigateBack 时有效,窗口显示/关闭动画的持续时间。App
render-linkbooleantrue是否给 navigator 组件加一层 a 标签控制 ssr 渲染web3.7.6+、App-vue3.7.6+
hover-classStringnavigator-hover指定点击时的样式类,当hover-class="none"时,没有点击态效果
hover-stop-propagationBooleanfalse指定是否阻止本节点的祖先节点出现点击态微信小程序
hover-start-timeNumber50按住后多久出现点击态,单位毫秒
hover-stay-timeNumber600手指松开后点击态保留时间,单位毫秒
targetStringself在哪个小程序目标上发生跳转,默认当前小程序,值域self/miniProgram微信2.0.7+、百度2.5.2+、QQ

open-type 有效值

说明平台差异说明
navigate对应 uni.navigateTo 的功能,保留当前页面,跳转到应用内的某个页面
redirect对应 uni.redirectTo 的功能,关闭当前页面,跳转到应用内的某个页面
switchTab对应 uni.switchTab 的功能,跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面
reLaunch对应 uni.reLaunch 的功能,关闭所有页面,打开到应用内的某个页面抖音小程序与飞书小程序不支持
navigateBack对应 uni.navigateBack 的功能,关闭当前页面,返回上一页面或多级页面
exit退出小程序,target="miniProgram"时生效微信2.1.0+、百度2.5.2+、QQ1.4.7+

2.api跳转(uni.navigateTo(对象))

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

page1代码

<template>
	<div>
		<!-- 组件跳转 -->
		<h1>我是page1页面</h1>
		<navigator url="/pages/page2/page2" hover-class="navigator-hover">
			<button type="default">跳转到新页面</button>
		</navigator>
		
		<button @click="gotopage2">使用uni.navigateTo方法api跳转</button>
	</div>
</template>

<script setup>
	const gotopage2=()=>{
		uni.navigateTo({
			url: "/pages/page2/page2"
		})
	}
</script>

<style></style>

实现效果

        

对象参数

参数类型必填默认值说明平台差异说明
urlString需要跳转的应用内非 tabBar 的页面的路径 , 路径后可以带参数。参数与路径之间使用?分隔,参数键与参数值用=相连,不同参数用&分隔;如 'path?key=value&key2=value2',path为下一个页面的路径,下一个页面的onLoad函数可得到传递的参数
animationTypeStringpop-in窗口显示的动画效果,详见:窗口动画App
animationDurationNumber300窗口动画持续时间,单位为 msApp
eventsObject页面间通信接口,用于监听被打开页面发送到当前页面的数据。2.8.9+ 开始支持。
successFunction接口调用成功的回调函数
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

具体详情可看官方文档

组件跳转navigator | uni-app官网https://uniapp.dcloud.net.cn/component/navigator.html

api跳转uni.navigateTo(OBJECT) | uni-app官网https://uniapp.dcloud.net.cn/api/router.html#navigateto

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐