question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can not access to component methods in page using `this.$refs`

See original GitHub issue

Environment

  • Operating System: Darwin
  • Node Version: v17.0.0
  • Nuxt Version: 3.0.0-27396610.ed4f4f5
  • Package Manager: npm@8.1.0
  • Bundler: Vite
  • User Config: css, build, publicRuntimeConfig, serverMiddleware
  • Runtime Modules: -
  • Build Modules: -

Reproduction

  1. Create a component and add a method like this:
// ~/components/player.vue
<script>
export default {
    methods: {
        play() {
            // Play something
        },
    },
}
</script>
  1. Create a page and use the player component and assign a ref to it:
// ~/pages/index.vue
<template>
    <Player ref="player" />
</template>
  1. Now in the page index.vue I should be able to access player component’s methods like this:
// ~/pages/index.vue
<script>
export default {
    mounted() {
        this.$refs.player.play()
    },
}
</script>

But it is not working and occurs an error:

Uncaught (in promise) TypeError: this.$refs.player.play is not a function

Describe the bug

In Nuxt 2 we can use a component’s methods in another component or page by using $refs. Currently, in Nuxt 3 I can not use this approach.

Additional context

No response

Logs

Uncaught (in promise) TypeError: this.$refs.player.play is not a function

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
danielroecommented, Feb 7, 2022

@alijmlzd Would you provide a reproduction using either codesandbox or stackblitz?

1reaction
alijmlzdcommented, Feb 3, 2022

I’m using $refs in the mounted state just like the guide link you mentioned but not works as expected. I have not tested this issue with Vue 3 standalone but in Nuxt 3 It obviously does not work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to access $refs in component method - Stack Overflow
When clicking the "Click me" element, the console logs show that the $refs of the component element are inaccessible. I think this might...
Read more >
[SOLVED] This.$refs.key returns undefined when it really is
I am using Vue 2.0 with Webpack. When I say console.log(this.$refs) (inside created() method inside a low level component) it retu…
Read more >
Refs and the DOM - React
Refs provide a way to access DOM nodes or React elements created in the render method. In the typical React dataflow, props are...
Read more >
Accessing Vue Component Data with $ref. - Medium
Component A has two methods; setThought that sets the value of a thought data property to any value passed from the editable div,...
Read more >
warning: function components cannot be given refs. attempts ...
Iget error 'Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?' . How...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found