Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

143
Vistas
Vue3 extend component using composition api. Its this possible?

I'm trying to create a dialog plugin in vue3 using composition api.

I'm at the step where i want to extend a base component.

Here is what I have:

I have created a live example here

// dialogInstaller.js
// component passed by user or some default components
import userComponent from "./userComponent.vue";

// helper mount function source: https://github.com/pearofducks/mount-vue-component
import { mount } from "@/mount";

// dialog related functions that must be accesible from the component above after extending
const dialogClose = ref(() => console.log("dialog closed")); // just an example

// extend the component
const dialogPop = {
  extends: userComponent,
  setup() {
    return {
      dialogClose,
      ...userComponent.setup(),
    };
  },
};

// now mount the component
const { vNode, destroyVNode, el } = mount(dialogPop, {
  props: {},
  app: appInstance, // vue app instance
  element: dialogWrapper, // component mount location
});

User component look like this:

<template>
  <div>
    <button type="button" @click="dialogClose">Close from template</button>
    <button type="button" @click="dialogCloseFromSetup">Close from setup</button>
    <button type="button" @click="dialogCloseFromInstance">Close from instance</button>
  </div>
</template>

<script>
import { ref, getCurrentInstance } from 'vue';
export default {
  name: 'userComponent',
  setup() {    
    const dialogCloseFromSetup = () => {
      dialogClose();
    };

    const instance = getCurrentInstance().ctx;        
    const dialogCloseFromInstance = () => {
      instance.dialogClose();
    };
    return { dialogCloseFromSetup, dialogCloseFromInstance };
  },
};
</script>

What i want to achieve is that the method "dialogClose" to be available from the userComponent -- template (like button 1) and setup (like button 2).

In other words i want that method to be accessible from every component that is passed to the dialog plugin. My head jump straight to the extend option because i used to use before in vue2

If button 1 is clicked everything working as expected, but the second button is not working, method is not accessible in setup.

I found a workaround to this problem by looking into the vue instance and accessing the method from "getCurrentInstance().ctx". everything work as espected if i do this (button 3).


I feel like this is not a good solution that's why im asking:

  • Its the way i extend the vue component correct ?
  • If its correct why can i access the method directly in the template and not in the setup?
  • Its a good idea to run the method from getCurrentInstance().ctx ?
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda