taro如何获取路径传参(3.0.9版本以上,解决办法:1版本降级,2getCurrentInstance() )
版本降级可以参考:https://www.cnblogs.com/yang-shun/p/12447377.htmlhttps://blog.csdn.net/c__chao/article/details/1048052543.0.9版本以上新写法,使用版本(3.0.15)在旧版本中可以通过 this.$router 访问当前组件/页面路由的详情。在 Taro Next 对应的 API 是在 @
·
-
版本降级
可以参考:
https://www.cnblogs.com/yang-shun/p/12447377.html
https://blog.csdn.net/c__chao/article/details/104805254 -
3.0.9版本以上新写法,作者使用版本(3.0.15)
在旧版本中可以通过 this.$router 访问当前组件/页面路由的详情。在 Taro Next 对应的 API 是在 @tarojs/taro 中的 getCurrentInstance().router,两者的属性一模一样。- 从@tarojs/taro引入getCurrentInstance
- current 变量获取getCurrentInstance()
- 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
有用点赞谢谢
更多推荐


所有评论(0)