介绍
-
目录
router
router跳转路由router.back返回router.goHome回到首页router.getOption获取参数-
详细
router
跳转路由可以完成
跳转路由
-
例
```javascript this.$router(‘pages/user/index’,{ // 复杂数据跳转 id:10, info:{ name:’张三’, age:19 } }).then(res=>{ console.log(‘跳转成功’) })
this.$router(‘pages/user/index?id=19&type=0&name=张飞’); // 简单数据跳转
this.$router(1,{name:10},’navigateBack’) // 后退1页
<a name="tnGt3"></a>### 参数| 参数 | 类型 | 是否必填 | 说明 || --- | --- | --- | --- || url | String/Number | Y | `String` :跳转地址 可以使用 `?&` 携带参数<br />`Number` :在 `mode` 为 `navigateBack` 时表示后退多少页 || data | Object | N | 携带数据,(在返回时依然可以携带参数,如果数据有同名则会覆盖 || mode | String | N | 跳转方式 ` ['navigateTo','redirectTo','reLaunch','navigateBack']` 之一 || config | Object | N | 其他配置详情:[https://uniapp.dcloud.io/api/router](https://uniapp.dcloud.io/api/router) |<a name="IOuEQ"></a>## router.goHome返回首页<a name="CrREH"></a>### 例```javascriptthis.$router.goHome() // 返回首页this.$router.goHome({name:10}) // 返回首页并携带参数
参数
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| data | Object | N | 传递的参数 |
router.back
例
this.$router.back(2,{name:'张三'}) // 后退两页并传入参数
参数
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| delta | Number | N | 后退几页( 默认 1) |
| data | Object | N | 携带参数 |
| config | Object | N | 其他配置 https://uniapp.dcloud.io/api/router?id=navigateback |
router.getOption
获取指定页面传入参数 (包括 通过 router 传递参数以及从 onLoad 传递的参数,如果有同名 onLoad 会覆盖使用 router 传递的参数)
例
console.log('传入参数',this.$router.getOption()) // 获取当前页面参数console.log('首页参数',this.$router.getOption('pages/index/index')) // 获取指定页面参数
参数
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| url | String | Y | String :页面地址 |
router.getPageInfo
返回值
{length, // 当前有多少个路由page, // 传入urlnowPage, // 当前urlisTabBar,// 传入url是不是 底部菜单isHome, // 传入url是不是 首页urlList, // 当前所有路由urloptionsList, // 所有路由 onLoad 传入参数options, // 传入url 通过 onLoad 方法传入的参}
其他可配置项
因为生成小程序太阳码有参数限制,所以增加解参函数。可在文件中配置 scene2Data 方法
scene2Data
function scene2Data(scene) {scene = decodeURIComponent(scene); // 解码scene = scene.split(','); // 分割if (scene.length === 0) return {};switch (scene[0]) {// 可以扩展,以下为例case '1': // ( type,用户id)return {SCENEDATA: {uid: scene[1], // 用户id},};case '2':return {SCENEDATA: {// (type,活动id)id: scene[1],},};default:return {};}}
参数
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| scene | String | Y | 小程序的参数字符串默认使用 , 拼接 第一个参用来区分类型。例: scene='1,uid,cid |
