加载中...
  • taro如何获取路径传参

    1.首先进行页面跳转传递参数

    1
    2
    3
    4
    // 跳转到目的页面,打开新页面
    Taro.navigateTo({
    url:'/pages/details/index?id=1'
    })

    2. 页面接收参数

    例子:
    类组件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    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>
    )
    }
    }

    函数式组件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    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的,写的相对简单,供自己下回查阅

    上一篇:
    _tarojs_taro__WEBPACK_IMPORTED_MODULE_1__.default.useState is not a function
    下一篇:
    前端单元测试介绍
    本文目录
    本文目录