组件二次封装时如何完美暴露expose方法 #13140
gaylen1
started this conversation in
General Discussions
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.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
组件二次封装时如何完美暴露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