- componentWillMount() //onLoad
- componentDidMount() //onReady 页面被装载(发送请求)
- componentWillUpdate state数据将要更新
- componentDidUpdate() state数据更新之后
- componentWillUnmount() //onUnload 页面即将卸载
- componentDidShow() //onShow 页面可见时触发
componentDidHide() //onHide 页面不可见时触发
class Home extends Component {
constructor(props) {
super(props);
// 页面初始数据
this.state = {}
}
// 页面配置
// 只在Page实例中存在的配置数据,对应于原生的page.json文件
config = {
navigationBarTitleText: '首页'
}
// 页面生命周期:在组件装载发生前被立刻调用
// 在小程序里对应的生命周期是 onLoad
// 避免在该方法中引入任何的副作用或订阅。对于这些使用场景,我们推荐使用 constructor()来替代。
componentWillMount() {
console.log('home componentWillMount')
}
// 页面生命周期:在组件被装载后立即调用
// 在微信小程序中这一生命周期方法对应 onReady
// 若你需要从远端加载数据,这是一个适合实现网络请求的地方。在该方法里 setState() 将会触发重新渲染。
componentDidMount() {
console.log('home componentDidMount')
console.log('home this:', this)
// 识别用户数据
// const userInfo = this.$root.$parent.globalData.userInfo
// if (userInfo) {
// this.userInfo = userInfo
// }
// 加载页面数据
this.loadPageData()
// 字典数据的遍历-demo
Object.values(Dict.TOWARDS).forEach(item => {
console.log(item)
})
// 工具函数-demo
console.log(Util.showTime())
}
// 页面生命周期:在已经装载的组件接收到新属性前调用
// 若你需要更新状态响应属性改变,你可能需对比 this.props 和 nextProps 并在该方法中使用 this.setState() 处理状态改变。
componentWillReceiveProps(nextProps) {
console.log('home componentWillReceiveProps', this.props, nextProps)
}
// 页面生命周期:当接收到新的 props 或 state 时,在渲染之前被调用。
// 用来判断是否需要重新渲染,默认返回true
shouldComponentUpdate() {
console.log('home shouldComponentUpdate')
}
// 页面生命周期:当接收到新的 props 或 state 时,在渲染之前立即被调用
// 该生命周期在 shouldComponentUpdate 返回true后被调用
componentWillUpdate() {
console.log('home componentWillUpdate')
}
// 页面生命周期:在更新发生后立即被调用
componentDidUpdate() {
console.log('home componentDidUpdate')
}
// 页面生命周期:在一个组件被 卸载(unmounted) 和 销毁(destroyed) 之前立即被调用
// 在微信小程序中这一生命周期方法对应 onUnload
componentWillUnmount() {
console.log('home componentWillUnmount')
}
// 页面生命周期:页面可见时时触发
// 在微信小程序中这一生命周期方法对应 onShow,在 H5 中同样实现
componentDidShow() {
console.log('home componentDidShow')
}
// 页面生命周期:页面不可见时触发
// 在微信小程序中这一生命周期方法对应 onHide,在 H5 中同样实现
componentDidHide() {
console.log('home componentDidHide')
}
// 微信小程序特有的页面事件:下拉刷新
onPullDownRefresh() {
console.log('home onPullDownRefresh')
}
// 微信小程序特有的页面事件:上拉触底
onReachBottom() {
console.log('home onReachBottom')
}
// 微信小程序特有的页面事件:点击右上角的转发
onShareAppMessage() {
console.log('home onShareAppMessage')
}
// 微信小程序特有的页面事件:触发页面滚动
onPageScroll() {
console.log('home onPageScroll')
}
// 微信小程序特有的页面事件:点击 tab 时触发
onTabItemTap() {
console.log('home onTabItemTap')
}
// 微信小程序特有的页面事件:预加载
componentWillPreload() {
console.log('home componentWillPreload')
}
}