1.首先进行页面跳转传递参数
// 跳转到目的页面,打开新页面
Taro.navigateTo({
url:'/pages/details/index?id=1'
})
2. 页面接收参数
例子:
类组件
import { Component } from 'react'
import { View, Text } from '@tarojs/components'
import { getCurrentInstance } from '@tarojs/taro'
export default class Index extends Component {
$instance = getCurrentInstance()
componentDidMount () {
console.log(this.$instance.router.params) // 页面参数获取
}
render () {
return (
<View className='index'>
<Text>详情页面</Text>
</View>
)
}
}
函数式组件
import React, { useState } from 'react'
import { View, Text } from '@tarojs/components'
import { getCurrentInstance } from '@tarojs/taro'
function Index(){
const [userName ,setUserName] = useState('Hello World!!!!')
const { router } = getCurrentInstance();
console.log(router.params) // 参数接收
return (
<View>
<Text>{userName}</Text>
</View>
)
}
export default Index
在控制台打印都是Ok的,写的相对简单,供自己下回查阅
网友评论