Fallback an exposed in Vue Composition API? #11849
Unanswered
silentmantra
asked this question in
Help/Questions
Replies: 1 comment
-
export function createHigherOrderExpose<T = any>(
elRef: Ref<any>,
subCompExposeKeys: string[],
customExpose: Record<string, any> = {},
): T {
return new Proxy(
[...subCompExposeKeys, ...Object.keys(customExpose)].reduce(
(obj, key) => ((obj[key] = undefined), obj),
{} as Record<string, any>,
),
{
get: (_, prop) => {
if (subCompExposeKeys.includes(prop as string)) {
return elRef.value?.[prop];
}
return customExpose[prop as keyof typeof customExpose];
},
},
) as T;
}
const LqTable = defineComponent({ ...{} });
const HigherOrderComponent = defineComponent({
setup(_, { expose }) {
const lqTableRef = ref();
// HigherOrderComponent expose
const customExpose = {};
expose(
createHigherOrderExpose(
lqTableRef,
[...['LqTable expose keys']],
customExpose,
),
);
return () =>
h(LqTable, {
ref: lqTableRef,
});
},
}); 使用 Proxy 来代理对子组件暴露的属性的访问,且被代理的对象的 key 必须要声明,否则会被 vue 忽略掉。 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have a table component with an exposed:
Works fine until I try to wrap the component into another with more high-level functionality:
The problem is that the exposed is lost and I have no idea how to forward the exposed up. I've tried to expose a ref of the wrapped component, proxies, no luck. Any way to expose the child component?
Beta Was this translation helpful? Give feedback.
All reactions