6

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?

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Doesn't this completely defy the purpose of using functional components?
0

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.

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.