I want to inject markup into existing components.
This is an easy example:
<!-- Arbitrary component X -->
<template>
<div>
<!-- I want a headline here -->
foo!
</div>
</template>
I know that I can achieve this by inserting a <slot/>
and then using <X><h1>Hello world!</h1></X>
. However, I want to do it dynamically without editing the original component.
So here's my idea using a higher-order-component:
import X from '~/components/X';
import injectHeadline from '~/hoc/injectHeadline.js';
export default {
components: {
X: injectHeadline(X, 'Hello world!')
}
}
with
<!-- injectHeadline.js -->
export default (component, headline) => Vue.component({
render(createElement) {
let result = createElement(component);
<!-- (*) somehow insert <h1>{{ headline }}</h1> here. How? -->
return result;
}
})
However, I had no luck manipulating the render result in (*)
. I tried fiddling with result.context.$children
, but that leads nowhere.
Any idea?