uniapp4条件编译,导航跳转

条件编译,跨端兼容

条件编译写法说明

#ifdef APP-PLUS
需条件编译的代码
#endif

仅出现在 App 平台下的代码

#ifndef H5
需条件编译的代码
#endif

除了 H5 平台,其它平台均存在的代码

#ifdef H5 || MP-WEIXIN
需条件编译的代码
#endif

在 H5 平台或微信小程序平台存在的代码(这里只有||,不可能出现&&,因为没有交集)

组件api样式各个的条件编译

<template>
	<view>
		<text>在组件中测试条件编译</text>
		<view>
			<!-- #ifdef MP-WEIXIN -->
			<view>
				只在微信小程序页面出现
			</view>
			<!-- #endif -->
			<!-- #ifdef H5 -->
			<view>
				只在浏览器页面出现
			</view>
			<!-- #endif -->
			<!-- #ifdef H5|| MP-WEIXIN -->
			<view>
				h5也出现 微信小程序也出现
			</view>
			<!-- #endif -->
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {

			}
		},
		// 在页面加载时打印
		// api测试条件编译
		onLoad() {
			// #ifdef H5
			console.log('在h5端打印')
			// #endif
			// #ifdef MP-WEIXIN
			console.log('在微信小程序中打印')
			// #endif
		},
		methods: {}
	}
</script>

<style>
	/* h5出现 */
	/* #ifdef H5 */
	view {
		color: #F3A73F;
		border: 1rpx solid #000000;
		padding: 10rpx;
		margin: 5rpx 20rpx 0;
	}
	/* #endif */
	/* 微信小程序出现 */
	/* #ifdef MP-WEIXIN */
	view {
		color: #87b58b;
		border: 1rpx solid #8f8fd6;
		padding: 10rpx;
		margin: 5rpx 20rpx 0;
	}
	/* #endif */
</style>

导航跳转传参 

声明式跳转

navigator页面跳转。

该组件类似HTML中的<a>组件,但只能跳转本地页面。目标页面必须在pages.json中注册。

该组件的功能有API方式,另见:https://uniapp.dcloud.io/api/router?id=navigateto(opens new window)

属性说明

属性名类型默认值说明平台差异说明
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
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 的功能
reLaunch对应 uni.reLaunch 的功能字节跳动小程序与飞书小程序不支持
navigateBack对应 uni.navigateBack 的功能
exit退出小程序,target="miniProgram"时生效微信2.1.0+、百度2.5.2+、QQ1.4.7+

 编程式跳转

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

OBJECT参数说明

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

导航跳转以及传参示例

<template>
	<view>
		<text>导航跳转</text>
		<!-- 声明式跳转:用组件进行跳转 -->
		<view>
			<text>声明式跳转:用组件进行跳转</text>
			<navigator url="../news/news" style="background-color: #F3A73F;padding: 10rpx;">跳转到新闻页面</navigator>
		</view>
		<view style="margin-top: 50rpx">
			<!-- navigator只能跳转至普通页面,要想跳转到tabbar页面需要添加open-type="switchTab"属性 -->
			<text>声明式跳转:用组件进行跳转,跳转到tabbar页面需要添加open-type="switchTab"属性</text>
			<navigator url="../../pages/me/me" style="background-color: #13f3b4;padding: 10rpx;" open-type="switchTab">跳转到我的页面</navigator>
		</view>
		<view style="margin-top: 50rpx">
			<text>声明式跳转:用组件进行跳转,前一个页面销毁添加open-type="reLaunch"属性</text>
			<navigator url="../news/news" style="background-color: #F3A73F;padding: 10rpx;" open-type="reLaunch">跳转到新闻页面</navigator>
		</view>
		<!-- 编程式跳转:用api进行跳转 -->
		<view style="margin-top: 50rpx">
			<text>编程式跳转:用api进行跳转</text>
			<view @click="tupian" style="background-color: #38552185;padding: 10rpx;">跳转到上传图片页面</view>
		</view>
		<view style="margin-top: 50rpx">
			<text>编程式跳转:用api进行跳转,跳转到tabbar页面uni.switchTab</text>
			<view @click="faxian" style="background-color: #0eb4bd85;padding: 10rpx;">跳转到发现页面</view>
		</view>
		<view style="margin-top: 50rpx">
			<text>编程式跳转:用api进行跳转,销毁上一个页面,回到主页uni.redirectTo</text>
			<view @click="bangding" style="background-color: #8cbbbd85;padding: 10rpx;">跳转到双向数据绑定页面</view>
		</view>
		<!-- 传参 -->
		<view style="margin-top: 50rpx;">
			<text>传参</text>
			<!-- 找到新闻页面进行接参 -->
			<navigator url="../news/news?id=10" style="background-color: #eac4f3;padding: 10rpx;">跳转到新闻页面</navigator>
			<navigator url="../news/news?id=10&age=19" style="background-color: #a6b7f3;padding: 10rpx; margin-top: 20rpx;">跳转到新闻页面</navigator>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {

			}
		},
		onUnload() {
			console.log('导航跳转主页面被销毁')
		},
		methods: {
			tupian() {
				console.log('1秒后跳转')
				setTimeout(res => {
					uni.navigateTo({
						url: '../shangchuantupian/shangchuantupian'
					})
					console.log('跳转完成')
				}, 1000)
			},
			faxian(){
				uni.switchTab({
					url:'../../pages/faxian/faxian'
				})
			},
			bangding(){
				uni.redirectTo({
					url:'../../subpages/home/home?id=66&age=22'
				})
			}
		}
	}
</script>

<style>

</style>

接收参数页面

<template>
	<view>
		新闻页面
	<view>
		<text selectable="true">新闻页面</text>
	</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		// 接参,通过onLoad的形参res进行接参
		onLoad(res){
			console.log(res)
		},
		onPullDownRefresh() {
			console.log('下拉刷新了')
		},
		methods: {
			
		}
	}
</script>

<style>

</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值