I'm using a my-link component to wrap an anchor tag on demand around various items. For that purpose a custom render method is used - however the createElement method can only create HTML nodes, creating plain text nodes does not seem to be possible.
Current scenario
Usage of my-link component
<template v-for="item in items">
<h4>
<my-link :url="item.url">{{ item.text }}</my-link>
</h4>
</template>
Implementation of my-link component as Link.vue
<script>
export default {
name: 'my-link',
props: { url: String },
render(createElement) {
if (this.url) {
return createElement(
'a', {
attrs: { href: this.url }
}, this.$slots.default
);
}
return createElement(
'span',
this.$slots.default
);
}
};
</script>
Resulting HTML
<h4>
<a url="/some-link">This item is linked</a>
</h4>
<h4>
<span>Plain text item</span>
</h4>
Desired scenario
The span tag in this particular scenario is superfluous and could be avoided - however, it's not clear to me, how and whether at all this is possible with Vue.js. In general, I'd like to know how to create plain text nodes using a custom render method.
<h4>
<a url="/some-link">This item is linked</a>
</h4>
<h4>
Plain text item
</h4>
Side-notes:
- question was originally raised for VueJS v2.1 & v2.2-beta (February 2017)
- focus was on proper semantic nodes (text node vs. element node)
createElement(null)created an empty VNode, but I could not find a way to properly wrap that in that scope.this._v(getChildrenTextContent(this.$slots.default))works like a charm, using an text extraction method similar to vuejs.org/v2/guide/render-function#Complete-Example. Please consider adding this as an answer here, with the remark of_v()probably being part of the internal API. Thanks!