Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

638
Views
Vue 3: Cómo acceder a la variable de configuración en la función de componente

Considere el siguiente ejemplo simple usando la API de composición en Vue 3. Estoy tratando de tener una instancia de test disponible en las funciones de mi componente.

 <script> import { defineComponent, ref, onMounted } from 'vue' export default defineComponent({ name: 'Test', setup(){ let test = ref() onMounted(() => { doSomething() }) return{ test, doSomething } } }) function doSomething(){ console.log(test) //<-- undefined console.log(this.test) //<-- undefined } </script>

¿Cómo accedo a la test dentro de doSomething() ? Según tengo entendido, todo lo que devuelva setup() debería estar disponible en todo el componente de forma similar a los atributos data() de la API de opciones.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

tienes que pasar la ref como parámetro

 <script> import { defineComponent, ref, onMounted } from 'vue' export default defineComponent({ name: 'Test', setup () { let test = ref(null) onMounted(() => { doSomething(test.value) }) return { test, doSomething } } }) function doSomething (param) { console.log(param); // null } </script>

otro enfoque:

 // functions.js import { ref } from 'vue' export let test = ref(null)
 // vue-file <script> import { defineComponent, ref, onMounted } from 'vue' import { test } from '../utils/functions.js' export default defineComponent({ name: 'Test', setup () { onMounted(() => { doSomething(test) }) return { test, doSomething } } }) function doSomething (param) { console.log(test.value); // <-- instant access console.log(param.value); // <-- import via parameter } </script>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!