Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useCurrentElement): new function
- Loading branch information
Showing
6 changed files
with
66 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<script setup lang="ts"> | ||
import { useCurrentElement } from '@vueuse/core' | ||
import { watchEffect } from 'vue' | ||
const el = useCurrentElement() | ||
watchEffect(() => { | ||
console.log('Current element:', el.value) | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div>Open your console.log to see the element</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
category: Component | ||
--- | ||
|
||
# useCurrentElement | ||
|
||
Get the DOM element of current component as a ref. | ||
|
||
## Usage | ||
|
||
```ts | ||
import { useCurrentElement } from '@vueuse/core' | ||
|
||
const el = useCurrentElement() // ComputedRef<Element> | ||
``` | ||
|
||
## Caveats | ||
|
||
This functions uses [`$el` under the hood](https://vuejs.org/api/component-instance.html#el). | ||
|
||
Value of the ref will be `undefined` until the component is mounted. | ||
|
||
- For components with a single root element, it will point to that element. | ||
- For components with text root, it will point to the text node. | ||
- For components with multiple root nodes, it will be the placeholder DOM node that Vue uses to keep track of the component's position in the DOM. | ||
|
||
It's recommend to only use this function for components with **a single root element**. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// eslint-disable-next-line no-restricted-imports | ||
import { computed, getCurrentInstance, onMounted, onUpdated, ref } from 'vue-demi' | ||
|
||
export function useCurrentElement<T extends Element = Element>() { | ||
const vm = getCurrentInstance()! | ||
const count = ref(0) | ||
onUpdated(() => { | ||
count.value += 1 | ||
}) | ||
onMounted(() => { | ||
count.value += 1 | ||
}) | ||
return computed<T>(() => { | ||
// force update | ||
// eslint-disable-next-line no-unused-expressions | ||
count.value | ||
return vm.proxy!.$el | ||
}) | ||
} |
e24db79
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
请教下,组件销毁的时候为什么不清空value呢,感觉有点不符合使用逻辑
e24db79
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
没考虑过,原本设计是在 setup 里用,组件销毁之后就没有意义了。的确语意不太对,这里的 Element 是 Reactive 的但是 Instance 不是。可能考虑改个名