0

I need to programatically create and mount components on the fly from parent component.

It works fine with Vue 2.

import Vue from 'vue' 

// ParentComponent.vue
export default {
  methods: {
    createChild() {
      const Child = Vue.extend(ChildComponent)
      const child = new Child({
        propsData,
        store: this.$store,
        i18n: this.$i18n
      }).$mount()
    }
  }
}

But I cannot figure out how-to do with Vue 3.

2
  • What about using in-template component rendering instead, like <component v-bind:is="childComponent"> instead? Commented Jul 1, 2021 at 14:09
  • I don't think this is a rendering issue, but more about having the flexibility to programatically instantiate components on the fly. Commented Jul 1, 2021 at 14:18

1 Answer 1

2

I have finally ended with this, after finding some info here:

// ParentComponent.vue
import { h, render } from 'vue' 

export default {
  methods: {
    createChild() {
      const child = h(ChildComponent, propsData)
      child.appContext = this.$.appContext  // use store, i18n
      const el = document.createElement('div')
      render(node, el)
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

what is your node variable? where is it set?

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.