• componentWillMount() //onLoad
    • componentDidMount() //onReady 页面被装载(发送请求)
    • componentWillUpdate    state数据将要更新
    • componentDidUpdate() state数据更新之后
    • componentWillUnmount() //onUnload 页面即将卸载
    • componentDidShow() //onShow 页面可见时触发
    • componentDidHide() //onHide 页面不可见时触发

      1. class Home extends Component {
      2. constructor(props) {
      3. super(props);
      4. // 页面初始数据
      5. this.state = {}
      6. }
      7. // 页面配置
      8. // 只在Page实例中存在的配置数据,对应于原生的page.json文件
      9. config = {
      10. navigationBarTitleText: '首页'
      11. }
      12. // 页面生命周期:在组件装载发生前被立刻调用
      13. // 在小程序里对应的生命周期是 onLoad
      14. // 避免在该方法中引入任何的副作用或订阅。对于这些使用场景,我们推荐使用 constructor()来替代。
      15. componentWillMount() {
      16. console.log('home componentWillMount')
      17. }
      18. // 页面生命周期:在组件被装载后立即调用
      19. // 在微信小程序中这一生命周期方法对应 onReady
      20. // 若你需要从远端加载数据,这是一个适合实现网络请求的地方。在该方法里 setState() 将会触发重新渲染。
      21. componentDidMount() {
      22. console.log('home componentDidMount')
      23. console.log('home this:', this)
      24. // 识别用户数据
      25. // const userInfo = this.$root.$parent.globalData.userInfo
      26. // if (userInfo) {
      27. // this.userInfo = userInfo
      28. // }
      29. // 加载页面数据
      30. this.loadPageData()
      31. // 字典数据的遍历-demo
      32. Object.values(Dict.TOWARDS).forEach(item => {
      33. console.log(item)
      34. })
      35. // 工具函数-demo
      36. console.log(Util.showTime())
      37. }
      38. // 页面生命周期:在已经装载的组件接收到新属性前调用
      39. // 若你需要更新状态响应属性改变,你可能需对比 this.props 和 nextProps 并在该方法中使用 this.setState() 处理状态改变。
      40. componentWillReceiveProps(nextProps) {
      41. console.log('home componentWillReceiveProps', this.props, nextProps)
      42. }
      43. // 页面生命周期:当接收到新的 props 或 state 时,在渲染之前被调用。
      44. // 用来判断是否需要重新渲染,默认返回true
      45. shouldComponentUpdate() {
      46. console.log('home shouldComponentUpdate')
      47. }
      48. // 页面生命周期:当接收到新的 props 或 state 时,在渲染之前立即被调用
      49. // 该生命周期在 shouldComponentUpdate 返回true后被调用
      50. componentWillUpdate() {
      51. console.log('home componentWillUpdate')
      52. }
      53. // 页面生命周期:在更新发生后立即被调用
      54. componentDidUpdate() {
      55. console.log('home componentDidUpdate')
      56. }
      57. // 页面生命周期:在一个组件被 卸载(unmounted) 和 销毁(destroyed) 之前立即被调用
      58. // 在微信小程序中这一生命周期方法对应 onUnload
      59. componentWillUnmount() {
      60. console.log('home componentWillUnmount')
      61. }
      62. // 页面生命周期:页面可见时时触发
      63. // 在微信小程序中这一生命周期方法对应 onShow,在 H5 中同样实现
      64. componentDidShow() {
      65. console.log('home componentDidShow')
      66. }
      67. // 页面生命周期:页面不可见时触发
      68. // 在微信小程序中这一生命周期方法对应 onHide,在 H5 中同样实现
      69. componentDidHide() {
      70. console.log('home componentDidHide')
      71. }
      72. // 微信小程序特有的页面事件:下拉刷新
      73. onPullDownRefresh() {
      74. console.log('home onPullDownRefresh')
      75. }
      76. // 微信小程序特有的页面事件:上拉触底
      77. onReachBottom() {
      78. console.log('home onReachBottom')
      79. }
      80. // 微信小程序特有的页面事件:点击右上角的转发
      81. onShareAppMessage() {
      82. console.log('home onShareAppMessage')
      83. }
      84. // 微信小程序特有的页面事件:触发页面滚动
      85. onPageScroll() {
      86. console.log('home onPageScroll')
      87. }
      88. // 微信小程序特有的页面事件:点击 tab 时触发
      89. onTabItemTap() {
      90. console.log('home onTabItemTap')
      91. }
      92. // 微信小程序特有的页面事件:预加载
      93. componentWillPreload() {
      94. console.log('home componentWillPreload')
      95. }
      96. }