Can not access to component methods in page using `this.$refs`
See original GitHub issueEnvironment
- 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
- Create a component and add a method like this:
// ~/components/player.vue
<script>
export default {
methods: {
play() {
// Play something
},
},
}
</script>
- Create a page and use the player component and assign a ref to it:
// ~/pages/index.vue
<template>
<Player ref="player" />
</template>
- 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:
- Created 2 years ago
- Comments:8 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@alijmlzd Would you provide a reproduction using either codesandbox or stackblitz?
I’m using
$refs
in themounted
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.