uni地址栏问号拼接传参接收参数
时间: 2023-07-03 20:11:44 浏览: 61
在uni-app中,可以通过地址栏问号拼接传参,然后在目标页面的onLoad生命周期函数中通过this.\$route.query来接收参数。具体步骤如下:
1. 在源页面中使用uni.navigateTo或uni.redirectTo跳转到目标页面时,在url中使用问号拼接参数,例如:
```
uni.navigateTo({
url: '/pages/targetPage/targetPage?id=1&name=test'
})
```
2. 在目标页面的onLoad生命周期函数中通过this.\$route.query来接收参数,例如:
```
onLoad: function (options) {
console.log(options.id) // 输出1
console.log(options.name) // 输出test
}
```
这样就可以在不同页面之间传递参数了。注意,如果使用uni.switchTab跳转到目标页面,则无法通过url传递参数,需要使用其他方式。
相关问题
uni-app get请求问号传参
你可以使用uni.request()函数来发送get请求并附带问号传参。以下是一个示例代码:
```
uni.request({
url: 'http://example.com/api/getData',
data: {
id: 123,
name: 'john'
},
success: function (res) {
console.log(res.data);
}
})
```
在上面的代码中,我们将请求的url设置为'http://example.com/api/getData',并通过data参数设置了两个问号传参'id'和'name'。在成功回调函数中,我们打印了响应数据res.data。
希望这能帮助到你!
uni.navigateto路由传参
uni.navigateTo路由传参可以通过在url后面添加参数的方式进行传参,但是需要注意参数的编码问题。可以使用encodeURIComponent()函数对参数进行编码,以避免特殊字符的解析问题。具体实现可以参考以下代码:
```javascript
// 在A页面中跳转到B页面,并传递参数
uni.navigateTo({
url: '/pages/B/B?id=' + encodeURIComponent('参数值')
})
// 在B页面中获取参数
export default {
onLoad(options) {
const id = decodeURIComponent(options.id)
console.log(id) // 输出:参数值
}
}
```
需要注意的是,在B页面中获取参数时,需要使用decodeURIComponent()函数对参数进行解码。另外,也可以通过在url中添加多个参数的方式进行传参,具体实现可以参考uni.navigateTo的官方文档。