# route 路由跳转

# 按需引入

如果未做全局扩展配置 (opens new window),可以按需引入:

// 引入
import route from '@/uni_modules/uv-ui-tools/libs/util/route.js';
// 调用
route(...);

# route(OBJECT)

路由跳转方法,内部是对uni多个路由跳转api的封装,更方便使用。支持路由拦截等多功能。

OBJECT参数说明:

参数名 类型 默认值 是否必填 说明
type String navigateTo false 路由跳转方式,参考下面Type Options
url String - false typenavigateBack时不填,其他必填
delta Number 1 false typenavigateBack时用到,表示返回的页面数
params Object - false 传递的对象形式的参数,如 { name: 'lisa', age: 18 }
animationType String pop-in false 只在APP生效,详见窗口动画 (opens new window)
animationDuration Number 300 false 动画持续时间,单位ms
events Object - false 页面间通信接口,用于监听被打开页面发送到当前页面的数据。hbuilderX 2.8.9+ 开始支持。详情参考uni.navigateTo (opens new window)

# Type Options

属性名 说明
navigateToto 同 uni.navigateTo()
redirectredirectTo 同 uni.redirectTo()
switchTabtab 同 uni.switchTab()
reLaunchlaunch 同 uni.reLaunch()
navigateBackback 同 uni.navigateBack()

# 基本用法

uni.$uv.route({
	url: '/subpages/demo/demo',
	params: {
		name: 'uvui'
	}
})

# 路由拦截

uni.$uv.route({
	url: '/subpages/demo/demo',
	intercept(params, next) {
		console.log(params, next)
		uni.showLoading({
			title: '等待中...'
		})
		// 模拟处理等待
		setTimeout(() => {
			uni.hideLoading();
			next(true);
		}, 1000)
	}
})
uni.$uv.route({
	url: '/subpages/demo/demo',
	events: {
		// 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
		acceptDataFromOpenedPage(data) {
			console.log(data)
		},
		someEvent(data) {
			console.log(data)
		}
	}
})

# 简写用法

为了方便简写和调用,可以直接传递一个url地址替代Object

注意:只能执行uni.navigateTo类型的地址,不支持跳转到Tabbar页面,如果有参数需要携带,以对象形式写到方法的第二个参数中。

// 无参数
uni.$uv.route('/subpages/demo/demo');

// 带参数
uni.$uv.route('/subpages/demo/demo', {
	name: 'uvui',
	age: 1
});