Skip to content

监听dom的ref失效 #9015

Aug 21, 2023 · 1 comments · 3 replies
Discussion options

You must be logged in to vote

我是这样用的

const _slider = ref<HTMLElement | null>()

watch(
    () => _slider.value,
    () => {
        console.log(222)
        if (_slider.value) {
            centerX.value = _slider.value?.getBoundingClientRect().width
            centerY.value = _slider.value?.getBoundingClientRect().height
            console.log(centerX.value, centerY.value)
        }
    }
)

The watch only can trigger the callback, when in the getter's reactive value is changed. The DOM change watch can't trigger the callback.I think you should get the DOM and then use the follow Browser API:

https://developer.mozilla.org/zh-CN/docs/Web/API/ResizeObserver

https://developer.mozilla.org/zh-CN/docs/Web/API/MutationOb…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@fishcy
Comment options

@liulinboyi
Comment options

Answer selected by fishcy
@fishcy
Comment options

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