Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Component template ref watcher triggers before its mounted hook is finished #12578

Closed
johannes-z opened this issue Jun 23, 2022 · 0 comments
Closed

Comments

@johannes-z
Copy link

johannes-z commented Jun 23, 2022

Version

2.7.0-beta.5

Reproduction link

github.com/johannes-z/vue27

Steps to reproduce

  1. git clone https://github.com/johannes-z/vue27
  2. git checkout bug/template-refs-mounted
  3. pnpm i
  4. pnpm run dev

What is expected?

The template ref watcher should be called once the mounted hook is finished.

What is actually happening?

The template ref watcher gets called immediately, so that data assigned in the mounted hook are not called yet and accessing public methods/properties are still undefined.


See the setup in InfiniteLoaderWrapper.vue. Using @vue/composition-api and vue ^2.6 the watcher would be called when the mounted hook is finished. With vue ^2.7 the watcher of the template ref is called immediately upon instantiation, so that vm.stateChanger is still undefined!

  setup (_, { emit }) {
    const infiniteLoader = ref(null)

    onMounted(() => {
      // prints object
      console.log(infiniteLoader.value.stateChanger);
    })

    const unwatch = watch(infiniteLoader, (vm: any) => {
      // prints undefined
      console.log(vm.stateChanger);
      if (!vm) return
      emit('infinite', vm.stateChanger)
      unwatch()
    })

    return {
      infiniteLoader,
    }
  }

This is how the property gets assigned in the library component I'm using: https://github.com/PeachScript/vue-infinite-loading/blob/master/src/components/InfiniteLoading.vue#L218

@johannes-z johannes-z changed the title Template ref watcher triggers before mounted is finished Component template ref watcher triggers before its mounted hook is finished Jun 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant