From 628da7aa966a5368161e0aa5c43dfa5946d82ad4 Mon Sep 17 00:00:00 2001 From: "Xc@centOs" Date: Thu, 2 Feb 2023 17:51:57 +0800 Subject: [PATCH] =?UTF-8?q?data=E4=BD=9C=E4=B8=BA=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E6=97=B6=E4=B9=9F=E9=9C=80=E8=A6=81callthis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/page.mp.js | 2 +- lib/page.web.js | 3 +-- lib/types/Page.d.ts | 2 +- src/page.mp.ts | 2 +- src/page.web.tsx | 3 +-- src/types/Page.ts | 2 +- 6 files changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/page.mp.js b/lib/page.mp.js index b5d27cbe..18340d71 100644 --- a/lib/page.mp.js +++ b/lib/page.mp.js @@ -505,7 +505,7 @@ function createComponent(option, features) { ready: function () { if (typeof data === 'function') { // ts的编译好像有问题,这里不硬写as过不去 - var data2 = data({ features: features, props: this.data }); + var data2 = data.call(this); this.setData(data2); } ready && ready.call(this); diff --git a/lib/page.web.js b/lib/page.web.js index 954e6dad..d37ad795 100644 --- a/lib/page.web.js +++ b/lib/page.web.js @@ -537,8 +537,7 @@ function createComponent(option, features) { _loop_1(m); } } - // ts似乎有个BUG,这里不硬写as会有编译错误 - var data2 = typeof data === 'function' ? data({ features: features, props: props }) : data; + var data2 = typeof data === 'function' ? data.call(_this) : data; _this.state = Object.assign({}, data2, { oakLoading: false, oakLoadingMore: false, diff --git a/lib/types/Page.d.ts b/lib/types/Page.d.ts index 02e0daf8..488930f1 100644 --- a/lib/types/Page.d.ts +++ b/lib/types/Page.d.ts @@ -74,7 +74,7 @@ export declare type ComponentPublicThisType void>; features: FD & BasicFeatures>; state: ComponentData; - props: ComponentProps; + props: Readonly>; setState: (data: Partial>, callback?: () => void) => void; triggerEvent: (name: string, detail?: DetailType, options?: WechatMiniprogram.Component.TriggerEventOption) => void; } & TMethod & OakCommonComponentMethods & OakListComponentMethods & OakSingleComponentMethods; diff --git a/src/page.mp.ts b/src/page.mp.ts index 7563c911..436d60d6 100644 --- a/src/page.mp.ts +++ b/src/page.mp.ts @@ -762,7 +762,7 @@ export function createComponent< ready() { if (typeof data === 'function') { // ts的编译好像有问题,这里不硬写as过不去 - const data2 = (data as ((option: { features: BasicFeatures> & FD, props: ComponentProps}) => TData))({ features, props: this.data as any }); + const data2 = (data as Function).call(this as any); this.setData(data2); } ready && ready.call(this); diff --git a/src/page.web.tsx b/src/page.web.tsx index a5a99051..dd337606 100644 --- a/src/page.web.tsx +++ b/src/page.web.tsx @@ -792,8 +792,7 @@ export function createComponent< } } - // ts似乎有个BUG,这里不硬写as会有编译错误 - const data2 = typeof data === 'function' ? (data as ((option: { features: BasicFeatures> & FD, props: ComponentProps }) => TData))({ features, props }) : data; + const data2 = typeof data === 'function' ? (data as Function).call(this as any) : data; this.state = Object.assign({}, data2, { oakLoading: false, oakLoadingMore: false, diff --git a/src/types/Page.ts b/src/types/Page.ts index fff717f0..91f280d2 100644 --- a/src/types/Page.ts +++ b/src/types/Page.ts @@ -165,7 +165,7 @@ export type ComponentPublicThisType< subscribed: Array<() => void>; features: FD & BasicFeatures>; state: ComponentData; - props: ComponentProps; + props: Readonly>; setState: ( data: Partial>, callback?: () => void,