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

299
Views
Vue composable: ¿cómo usar varias instancias de composable sin compartir el estado?

Tengo un archivo componible en mi aplicación VueJS:

 // simple example of a composable that fetch some information from API and shows that information 
// in a table in two components at the sime time
export function useDatatable () {
 const table = ref({
 headers: [...],
 items: [],
 someValue: ''
 })

 async function getDocuments () {
 const { data } = await $axios.get('/documents')
 table.value.items = data
 }

 return {
 table,
 getDocuments
 }
}

Entonces tengo múltiples componentes que usan este componible al mismo tiempo:

 <template>
 <div>
 <document-table /> // composable is used here
 <document-billing-dialog /> // composable is used here too
 </div>
</template>

Luego, en ambos componentes ( document-table y document-billing-dialog ) utilizo este componible así:

 <template>
 <div>
 {{ table.someValue }}
 </div>
 <v-table :items="table.items" />
 <v-btn @click="getDocuments">
 Reload table
 </v-btn>
 // other components
</template>

<script>
 import { useDatatable } from '~/composables/useDatatable'
 // other imports

 export default defineComponent({
 setup () {
 const { table, getDocuments } = useDatatable()

 onMounted(() => { getDocuments() })

 return {
 table,
 getDocuments
 }
 }
 })
</script>

Sin embargo, cuando 1 componente llama a la función getDocuments , se llama dos veces porque se usa en dos componentes al mismo tiempo.

Otro ejemplo es que si cambio el valor de table.value.someValue = 'something' cambia en ambos componentes.

¿Hay alguna forma de tener múltiples instancias de un componible al mismo tiempo sin compartir el estado?

almost 4 years ago · Santiago Trujillo
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!