uni-app 页面传参实战

本文详细介绍了UniApp中各种页面跳转的方法及其区别,包括保留当前页面的跳转、不保留当前页面的跳转、关闭所有页面重新打开以及跳转到tabBar页面等,并给出了具体的实现代码示例。

页面跳转

保留当前页面并跳转新页面

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

例子:

跳转到 test 文件夹下的 test 页面

uni.navigateTo({
    url: '../test/test'
});

不保留当前页面并跳转新页面

关闭当前页面,跳转到应用内的某个页面。

例子:

跳转到 test 文件夹下的 test 页面

uni.redirectTo({
    url: '../test/test'
});

关闭所有页面并打开一个页面

关闭所有页面,打开到应用内的某个页面。

例子:

跳转到 test 文件夹下的 test 页面

uni.reLaunch({
    url: '../test/test'
});

跳转到 tabBar 页面

只能使用 switchTab 跳转,并关闭其他所有非 tabBar 页面。

pages.json 中配置的 tabBar
{
  "tabBar": {
    "list": [{
      "pagePath": "pages/index/index",
      "text": "首页"
    },{
      "pagePath": "pages/other/other",
      "text": "其他"
    }]
  }
}

other.vue 页面切换
uni.switchTab({
    url: '/pages/index/index'
});

返回上一个页面或者返回第几个页面

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

// 注意:调用 navigateTo 跳转时,调用该方法的页面会被加入堆栈,而 redirectTo 方法则不会。见下方示例代码

// 此处是A页面
uni.navigateTo({
    url: 'B?id=1'
});

// 此处是B页面
uni.navigateTo({
    url: 'C?id=1'
});

// 在C页面内 navigateBack,将返回A页面
uni.navigateBack({
    delta: 2
});

页面传参

  • 现在<template>中获取数据
:data-postid="item.post_id" 就是要传递的 数据,通过 `:data-`开头+参数即可
<template>
	<view>
		<view class="page-section indexListBox">
			<view class="indexList" v-for="(item , index) in homePosts" :key="index">
				<view class="button-to-detail">
					<button @tap="onItemClick" :data-postid="item.post_id">点击查看详情</button>
				</view>
			</view>
		</view>
	</view>
</template>
  • <script>中得到点击的数据,接着在页面跳转时传递
onItemClick(e) {
	let post_id = e.currentTarget.dataset.postid
	uni.navigateTo({
		url: '../detail/detail?post_id=' + post_id
	})
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_龙衣

赏杯快乐水喝喝

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值