Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
54
rated 0 times [  59] [ 5]  / answers: 1 / hits: 16561  / 4 Years ago, wed, august 5, 2020, 12:00:00

I am using Vue 3 beta version and I am trying to access ref in setup function, My component:


JS(Vue):




const Child = createComponent({
setup () {
let tabs = ref()
console.log(tabs)
return {}
},
template: '<div ref=tabs >Wow</div>'
})




Demo: https://jsfiddle.net/xkeyLwu4/2/


But the value of tabs.value is undefined. What I am doing wrong here?


More From » vue.js

 Answers
43

  1. You need to have setup() return a ref with the same name.



  2. You can't log the DOM result until after mounting (onMounted)




const Child = createComponent({
setup () {
let tabs = ref()
onMounted(() => console.log(tabs.value))
return { tabs }
},
template: '<div ref="tabs" >Wow</div>'
})

See the docs for more examples: https://composition-api.vuejs.org/api.html#template-refs


[#50739] Friday, July 24, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pranavrorys

Total Points: 466
Total Questions: 87
Total Answers: 115

Location: Barbados
Member since Sun, Nov 27, 2022
2 Years ago
;