Hamed Ehtesham
Hamed Ehtesham

Reputation: 147

Vue.js: instantiate functional component programmatically

when I instantiate functional component using this code

const Component_Constructor = Vue.extend(Component);
let component_instance = new Component_Constructor();
component_instance.$mount();

the component gets undefined context argument on the render function

how can i pass parameters (props, slots, children, ...) to the component?

Upvotes: 6

Views: 883

Answers (2)

Pico12
Pico12

Reputation: 1279

As one of the vuejs core team said here https://forum.vuejs.org/t/functional-component-with-vue-extend/40752, you can not mount functional component manually.

$mount() need a vue instance, that a functional component does not have.

Upvotes: 0

Hamed Ehtesham
Hamed Ehtesham

Reputation: 147

the only workaround I found so far is to wrap the functional component into another normal component like this:

let AComponent = {
    functional: true,
    name: 'a-component',
    render(h, context) {
        return h('div', context.children[0].text);
    }
};
let template = `<a-component>test content</a-component>`;
let WrapperComponent = Vue.extend({
    components: {AComponent},
    template,
});
let componentInstance = new WrapperComponent().$mount();
let content = componentInstance.$el;

Upvotes: 2

Related Questions