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.
my-link
component<template v-for="item in items">
<h4>
<my-link :url="item.url">{{ item.text }}</my-link>
</h4>
</template>
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>
<h4>
<a url="/some-link">This item is linked</a>
</h4>
<h4>
<span>Plain text item</span>
</h4>
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:
Open the file in your code editor. Create the component's template section by adding <template></template> to the top of the file. Create a <script></script> section below your template section. Inside the <script> tags, add a default exported object export default {} , which is your component object.
A render function returns a virtual DOM node, commonly named VNode in the Vue ecosystem, which is an interface that allows Vue to write these objects in your browser DOM. They contain all the information necessary to work with Vue.
js. Love it or hate it, JSX is a popular extension to JavaScript that allows XML tokens in your scripts. If you want to create templates in your script files and you find Vue's render() function to be difficult to work with, JSX may be just what you need.
Vue uses an HTML-based template syntax that allows you to declaratively bind the rendered DOM to the underlying component instance's data. All Vue templates are syntactically valid HTML that can be parsed by spec-compliant browsers and HTML parsers.
Vue exposes an internal method on it's prototype called _v that creates a plain text node. You can return the result of calling this method from a render function to render a plain text string:
render(h){
return this._v("my string value");
}
Exposing it in this way, prefixed with an underscore, likely indicates it's intended as a private API method, so use with care.
If you use a functional component, "this" is also not available. In this case, you should call context._v(), for example:
functional: true,
render(h, context){
return context._v("my string value")
}
This, combined with extracting the text from the slot (as in your comment, using the helpful getChildrenTextContent) will produce the desired result.
You can actually get around having to use the _v method mentioned in answers above (and potentially avoid using an internal Vue method that might get renamed later) by changing your implementation of the the MyLink component to be a functional component. Functional components do not require a root element, so it would get around having to put a span around the non-link element.
MyLink could be defined as follows:
const MyLink = {
functional: true,
name: 'my-link',
props: { url: String },
render(createElement, context) {
let { url } = context.props
let slots = context.slots()
if (url) {
return createElement(
'a', {
attrs: { href: url }
}, slots.default
);
}
else {
return slots.default
}
}
};
Then to use it, you could do something like this in a different component:
<div>
<h4 v-for="item in items">
<my-link :url="item.url">{{ item.text }}</my-link>
</h4>
</div>
See: https://codepen.io/hunterae/pen/MZrVEK?editors=1010
Also, as a side-note, it appears your original code snippets are naming the file as Link.vue. Since you are defining your own render function instead of using Vue's templating system, you could theoretically rename the file to Link.js and remove the beginning and closing script tags and just have a completely JS component file. However, if you component includes custom systems (which your snippet did not), this approach will not work. Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With