data作为函数时也需要callthis

This commit is contained in:
Xu Chang 2023-02-02 17:51:57 +08:00
parent 44868cb150
commit 628da7aa96
6 changed files with 6 additions and 8 deletions

View File

@ -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);

View File

@ -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,

2
lib/types/Page.d.ts vendored
View File

@ -74,7 +74,7 @@ export declare type ComponentPublicThisType<ED extends EntityDict & BaseEntityDi
subscribed: Array<() => void>;
features: FD & BasicFeatures<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>;
state: ComponentData<ED, T, FormedData, TData>;
props: ComponentProps<IsList, TProperty>;
props: Readonly<ComponentProps<IsList, TProperty>>;
setState: (data: Partial<ComponentData<ED, T, FormedData, TData>>, callback?: () => void) => void;
triggerEvent: <DetailType = any>(name: string, detail?: DetailType, options?: WechatMiniprogram.Component.TriggerEventOption) => void;
} & TMethod & OakCommonComponentMethods<ED, T> & OakListComponentMethods<ED, T> & OakSingleComponentMethods<ED, T>;

View File

@ -762,7 +762,7 @@ export function createComponent<
ready() {
if (typeof data === 'function') {
// ts的编译好像有问题这里不硬写as过不去
const data2 = (data as ((option: { features: BasicFeatures<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>> & FD, props: ComponentProps<IsList, TProperty>}) => TData))({ features, props: this.data as any });
const data2 = (data as Function).call(this as any);
this.setData(data2);
}
ready && ready.call(this);

View File

@ -792,8 +792,7 @@ export function createComponent<
}
}
// ts似乎有个BUG这里不硬写as会有编译错误
const data2 = typeof data === 'function' ? (data as ((option: { features: BasicFeatures<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>> & FD, props: ComponentProps<IsList, TProperty> }) => 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,

View File

@ -165,7 +165,7 @@ export type ComponentPublicThisType<
subscribed: Array<() => void>;
features: FD & BasicFeatures<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>;
state: ComponentData<ED, T, FormedData, TData>;
props: ComponentProps<IsList, TProperty>;
props: Readonly<ComponentProps<IsList, TProperty>>;
setState: (
data: Partial<ComponentData<ED, T, FormedData, TData>>,
callback?: () => void,