1. 版本降级
    可以参考:
    https://www.cnblogs.com/yang-shun/p/12447377.html
    https://blog.csdn.net/c__chao/article/details/104805254

  2. 3.0.9版本以上新写法,作者使用版本(3.0.15)
    在旧版本中可以通过 this.$router 访问当前组件/页面路由的详情。在 Taro Next 对应的 API 是在 @tarojs/taro 中的 getCurrentInstance().router,两者的属性一模一样。

    1. 从@tarojs/taro引入getCurrentInstance
    2. current 变量获取getCurrentInstance()
    3. this.current.router获取
import { getCurrentInstance } from '@tarojs/taro'
class C extends Component {
  current = getCurrentInstance()

  componentWillMount () {
    // getCurrentInstance().router 和 this.$router 和属性一样
    console.log(this.current.router)
  }
}

// 函数式组件
import { getCurrentInstance } from '@tarojs/taro'
function C () {
  const { router } = getCurrentInstance()
  // getCurrentInstance().router 和 useRouter 返回的内容也一样
  // const router = useRouter()
}

而对于项目入口组件而言,路由信息我们推荐在 componentDidShow 生命周期的参数中直接读取。

// app.js 项目入口文件
class App extends Component {
  componentDidShow (options /* 这里有你想要的路由信息 */) {
  }

  render () {
    ...
  }
}

聪明的读者已经猜到了,getCurrentInstance().router 其实是访问小程序当前页面 onLoad 生命周期参数的快捷方式。

参考自:官网https://taro-docs.jd.com/taro/docs/migration

有用点赞谢谢

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐