Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

组件二次封装时如何完美暴露expose方法 #13140

gaylen1 started this conversation in General Discussions
Discussion options

组件二次封装时如何完美暴露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);

还有没有更加优雅的方式?

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

const input = useTemplateRef('inputRef');
defineExpose(
 new Proxy({}, {
 get(_, key) {
 return input.value?.[key];
 },
 has(_, key) {
 return key in (input.value || {});
 }
 })
);
You must be logged in to vote
1 reply
Comment options

这样确实可以调用到组件暴露的方法,但是在打印的时候不知道他有那些方法。
image
看上去不是特别完美,还有没有跟好的。谢谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /