0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Element PlusのExposesを理解した

Posted at

いつもelement plusを使っていると、コンポーネント内の子要素を操作することに手間がかかっていた。

ドキュメントのコード例では書いていなさそう。

毎回ref指定してquerySelectorだったり、Vanilla JSでDOM操作するしかないと思いつつ作業していたが、ドキュメントで以下のようなものを発見した。

cascaderのドキュメント

スクリーンショット 2024-06-17 18.16.16.png

Descriptionをcascader panel refcascader content refで書いているということは、要素をrefで渡しているように見える。

Exposes項目に入っているので、vueのdefineExposeを指すと考えた。
defineExposeは子要素から親要素に渡すためのもので、refから.でその要素を指定すると使うことができる。

コード例

ElementPlus.vue
<script setup lang="ts">
const cascader = ref();

const suggestedElement = categoryCascader.value.contentRef.querySelector(
    '.el-cascader__suggestion-item',
) as HTMLElement;

onMounted(() => {
    suggestedElement.focus(); // mount時にcascaderにfocusする
});
</script>
<template>
    <el-cascader ref="cascader"></el-cascader>
</template>

終わりに

ドキュメントに何か書いてあるのに今必要なものだけに集中すると、時間の無駄をしてしまうことがわかった。ドキュメント大事。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?