由于微信小程序里页面在 onLoad 时才能拿到页面的路由参数,而页面 onLoad 前组件都已经 attached 了。因此页面的 componentWillMount 可能会与预期不太一致。例如:
// 错误写法render () {// 在 willMount 之前无法拿到路由参数const abc = this.$router.params.abcreturn <Custom adc={abc} />}// 正确写法componentWillMount () {const abc = this.$router.params.abcthis.setState({abc})}render () {// 增加一个兼容判断return this.state.abc && <Custom adc={abc} />}
对于不需要等到页面 willMount 之后取路由参数的页面则没有任何影响。
