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

297
Vistas
Vue3 onMounted function doesn’t work in esm package bundled with rollup

I bundled a vue3 component with rollup. The component is simple:

// main.vue
<template>
  <div>
    <div @click="clickDiv">{{ 1 }}</div>
    <div ref="divRef"></div>
  </div>
</template>
<script>
import { onMounted, ref, defineComponent } from 'vue'
export default defineComponent({
  name: 'Vue3Component',
  mounted () {
    console.log('component mounted') // it works well
  },
  setup() {
    const divRef = ref()
    const clickDiv = () => {
      console.log('divRef', divRef) // it causes some warnings
    }
    onMounted(() => {
      console.log('component onMounted') // it causes some warnings
    })
    return {
      a: ref(1),
      clickDiv,
      divRef
    }
  },
})
</script>

and the input entry:

// index.js
import Vue3Component from './main.vue'
Vue3Component.install = (app) => {
  app.component(Vue3Component.name, Vue3Component)
}

export {
  Vue3Component
}

rollup config code:

// rollup.config.js
import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import pkg from './package.json'
import external from 'rollup-plugin-peer-deps-external'
import vuePlugin from 'rollup-plugin-vue'

const extensions = ['.vue', '.js']

const globals = {
  vue: 'Vue',
  'highlight.js': 'hljs',
  'marked': 'marked'
}

export default [
  {
    input: 'src/index.js',
    output: [
      {
        name: 'VueNextMEditor',
        file: pkg.main,
        format: 'umd',
        globals
      },
      {
        file: pkg.module,
        format: 'es'
      },
      {
        name: 'VueNextMEditor',
        file: pkg.unpkg,
        format: 'umd',
        plugins: [terser()],
        globals
      }
    ],
    plugins: [
      external(),
      vuePlugin(),
      resolve(),
      commonjs({ extensions })
    ]
  }
]

When i test the component locally with vue-cli, the onMounted in setup doesn't works. The browser shows error like this:

[Vue warn]: onMounted is called when there is no active component instance to be associated with. Lifecycle injection APIs can only be used during execution of setup(). If you are using async setup(), make sure to register lifecycle hooks before the first await statement.
[Vue warn]: Missing ref owner context. ref cannot be used on hoisted vnodes. A vnode with ref must be created inside the render function. 
  at <Vue3Component> 
  at <App>

But mounted outside the setup function works will. Anyone can help me to resolve this problem? Vue3Component

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I resolved the problem. It happenes because I install vue twice. I resolved it with pnpm link

$ cd vue3-component
$ pnpm install

$ cd example
$ pnpm link ../node_modules/vue
$ pnpm install
$ pnpm serve

It works!

about 4 years ago · Juan Pablo Isaza Denunciar
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