2

I want to do this in Vue 3

new ComponentName({ 
  propsData: {
    title: 'hello world',
  }
}).$mount();

But I'm getting this error: VueComponents_component_name__WEBPACK_IMPORTED_MODULE_1__.default is not a constructor

Currently, we are using the above approach to append VUE components in our legacy app via append

I would like to do the same on VUE 3 but I haven't found the way to do it

Thanks in advance

1
  • you can't mount a component, you need to mount the app and add the component to the app. see docs Commented Feb 3, 2021 at 20:51

3 Answers 3

13

I found the solution to my answer, mounting a vue component in vue 3 (Outside vue projects) is different than vue 2, this is the approach :

// mount.js

import { createVNode, render } from 'vue'

export const mount = (component, { props, children, element, app } = {}) => {
    let el = element

    let vNode = createVNode(component, props, children)
    if (app && app._context) vNode.appContext = app._context
    if (el) render(vNode, el)
    else if (typeof document !== 'undefined' ) render(vNode, el = document.createElement('div'))

    const destroy = () => {
        if (el) render(null, el)
        el = null
        vNode = null
    }

    return { vNode, destroy, el }
}
  • el: DOM element to be appended
  • vNode: Vue instance
  • destroy: Destroy the component

This is the way to mount vue 3 components to be appended directly to the DOM, and can be used as below:

// main.js

import { mount } from 'mount.js'

const { el, vNode, destroy } = mount(MyVueComponents,
      {
          props: {
            fields,
            labels,
            options
           },
           app: MyVueApp
      },
)

$element.html(el);

Hope it Helps, regards!

Sign up to request clarification or add additional context in comments.

3 Comments

Hello ! Is it possible to have more context ? I was using this part of code with Vue 2 in a method of mixin, that I load in my app. I have the same error, but I don't understand some things.. How do you import the mount function ? And from where you get the instance app to pass in arguments of mount ? Thanks in advance !
how does this $element appear?
The instance app MyVueApp can be imported from App.Vue that is usually found at the root of the project. The app is just a Vue component that is defined using defineComponent and exported. So bottom line you can do import MyVueApp from '../../App'
1

Just for future visitors to save some time, I was searching for the same answer and found a plug-in that does exactly what Luis explained en his answer at https://github.com/pearofducks/mount-vue-component

Makes it a little simpler to implement.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
1

It is easy to create a new vue3 app and mount to a DOM directly,

    const appDef = {
        data() {
            return {title: 'hello world'};
        },
        template: '<div>title is: {{title}}</div>',
    }

    var el = document.createElement('div');//create container for the app

    const app = Vue.createApp(appDef);
    app.mount(el);//mount to DOM
    
    //el: DOM element to be appended
    console.log(el.innerHTML);//title is: hello world

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.