@@ -5,8 +5,8 @@ let self: any = null
5
5
6
6
const popStateCallback = ( ) => {
7
7
// 使用popStateFn保存函数防止addEventListener重复注册
8
- if ( self && self . getInitialProps ) {
9
- self . getInitialProps ( )
8
+ if ( self && self . asyncData ) {
9
+ self . asyncData ( )
10
10
}
11
11
}
12
12
@@ -29,20 +29,18 @@ interface IState {
29
29
export default ( SourceComponent : any ) =>
30
30
class HoComponent extends React . PureComponent < IProps , IState > {
31
31
state = {
32
- initialData : { } ,
33
- canClientFetch : false , // 浏览器端是否需要请求数据
34
32
page : {
35
33
title : '' ,
36
34
keywords : '' ,
37
35
description : ''
38
- }
36
+ } ,
37
+ initialData : { } ,
38
+ canClientFetch : false // 浏览器端是否需要请求数据
39
39
}
40
40
41
41
// 转接子组件的预取方法,服务端会调用这个方法来做数据预取
42
- static async getInitialProps ( ctx : any ) {
43
- return SourceComponent . getInitialProps
44
- ? await SourceComponent . getInitialProps ( ctx )
45
- : { }
42
+ static async asyncData ( ctx : any ) {
43
+ return SourceComponent . asyncData ? await SourceComponent . asyncData ( ctx ) : { }
46
44
}
47
45
48
46
async componentDidMount ( ) {
@@ -53,18 +51,18 @@ export default (SourceComponent: any) =>
53
51
const canClientFetch = history && history . action === 'PUSH' // 路由跳转的时候可以异步请求数据
54
52
if ( canClientFetch ) {
55
53
// 如果是 history PUSH 操作 则更新数据
56
- await this . getInitialProps ( )
54
+ await this . asyncData ( )
57
55
}
58
56
}
59
57
60
- // 约定所有页面组件内的数据预取方法为getInitialProps ,用于双端调用
61
- async getInitialProps ( ) {
62
- // ssr首次进入页面以及csr/ssr切换路由时才调用组件的getInitialProps方法
58
+ // 约定所有页面组件内的数据预取方法为asyncData ,用于双端调用
59
+ async asyncData ( ) {
60
+ // ssr首次进入页面以及csr/ssr切换路由时才调用组件的asyncData方法
63
61
const store = window . __STORE__ // 从全局得到 store
64
62
// 兼容不使用 redux 的页面
65
63
// 通过props.getInitialData判断
66
- const res = SourceComponent . getInitialProps
67
- ? await SourceComponent . getInitialProps ( { store } )
64
+ const res = SourceComponent . asyncData
65
+ ? await SourceComponent . asyncData ( { store } )
68
66
: { }
69
67
// 处理页面 title 显示
70
68
const { tdk } = res . page || { }
0 commit comments