Skip to content

Commit

Permalink
fix: fix that cannot access computed by this.requestStates.computedValue
Browse files Browse the repository at this point in the history
  • Loading branch information
JOU-amjs committed Jul 18, 2024
1 parent 58c744e commit 8d6600a
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 185 deletions.
1 change: 0 additions & 1 deletion packages/shared/src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,6 @@ export function statesHookHelper<AG extends AlovaGenerics>(
referingObject.trackedKeys[key] = trueValue;
return isFrameworkState ? value.e : value;
},
set: noop,
enumerable: trueValue,
configurable: trueValue
});
Expand Down
19 changes: 17 additions & 2 deletions packages/vue-options/src/mapAlovaHook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isPlainObject } from '@alova/shared/function';
import { ObjectCls } from '@alova/shared/vars';
import { computed, reactive, version } from 'vue';
import { UseHookCallers, UseHookMapGetter, VueHookMapperMixin } from '~/typings';
import { classifyHookExposure, extractWatches } from './helper';
Expand Down Expand Up @@ -44,9 +45,23 @@ export default <UHC extends UseHookCallers>(mapGetter: UseHookMapGetter<UHC>) =>
myAssert(/^3|2\.7/.test(version), 'please upgrade vue to `2.7.x` or `3.x`');

// 挂载响应式状态和计算属性
vm[dataKey] = reactive(states);
const proxy = {};
vm[dataKey] = proxy;
const rectiveStates = reactive(states);
for (const key in states) {
ObjectCls.defineProperty(proxy, key, {
get: () => rectiveStates[key],
set: value => {
rectiveStates[key] = value;
return value;
}
});
}
for (const key in computeds) {
vm[dataKey][key] = computed(() => computeds[key]());
const computedState = computed(computeds[key]);
ObjectCls.defineProperty(proxy, key, {
get: () => computedState.value
});
}

// 设置watchers
Expand Down
16 changes: 12 additions & 4 deletions packages/vue-options/test/components/Pagination.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<span role="pageCount">{{ paging.pageCount }}</span>
<span role="total">{{ paging.total }}</span>
<span role="isLastPage">{{ paging.isLastPage }}</span>
<span role="response">{{ JSON.stringify(paging.data) }}</span>
<span role="error">{{ paging.error?.message }}</span>
<span role="response">{{ stringify(paging.data) }}</span>
<span role="error">{{ paging.error ? paging.error.message : ' }}</span>
<button
role="setPage"
@click="paging$update({ page: paging.page + 1 })">
Expand All @@ -20,7 +20,7 @@
</button>
<button
role="setLastPage"
@click="paging.page = paging.pageCount || 1">
@click="handleSetLastPage">
btn3
</button>
</div>
Expand All @@ -44,6 +44,14 @@ export default {
return {
paging: usePagination(this.getter, this.paginationConfig)
};
})
}),
methods: {
stringify(data) {
return JSON.stringify(data);
},
handleSetLastPage() {
this.paging.page = this.paging.pageCount;
}
}
};
</script>
Loading

0 comments on commit 8d6600a

Please sign in to comment.