组件二次封装时如何完美暴露expose方法 #13140
gaylen1
started this conversation in
General Discussions
组件二次封装时如何完美暴露expose方法
#13140
-
组件二次封装时如何完美暴露expose方法
举个列子
例如elementplus框架的el-input组件ref暴露的方法大家应该都是用这个原理实现对外暴露的。
const input= ref();
const expose = {};
onMounted(() => {
const entries = Object.entries(input.value);
for (const [method, fn] of entries) {
expose[method] = fn;
}
});
defineExpose(expose);
还有没有更加优雅的方式?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 1 reply
-
const input = useTemplateRef('inputRef'); defineExpose( new Proxy({}, { get(_, key) { return input.value?.[key]; }, has(_, key) { return key in (input.value || {}); } }) );
Beta Was this translation helpful? Give feedback.
All reactions
1 reply
-
这样确实可以调用到组件暴露的方法,但是在打印的时候不知道他有那些方法。
image
看上去不是特别完美,还有没有跟好的。谢谢
Beta Was this translation helpful? Give feedback.
All reactions
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment