Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
47
rated 0 times [  48] [ 1]  / answers: 1 / hits: 33035  / 7 Years ago, sun, march 12, 2017, 12:00:00

I have a vue component that uses template, I'd like to change it to use render function, I know createElement('h1', 'title'), but how to use it with something like 'select' with all the options? here is the template based component:



https://jsfiddle.net/aaoehLqe/



<div id=app>
<p>{{ message }}</p>
<myselect v-bind:option= option ></myselect>
{{option}}
</div>

More From » vue.js

 Answers
57

Here is the select component with createElement:


Vue.component('myselect', {
props: ['option'],
render: function(createElement) {
var self = this
return createElement(
'select', {
domProps: {
value: self.option.value
},
on: {
input: function(event) {
self.option.value = event.target.value
}
}
}, [
createElement('option', {
attrs: {
value: 0
}
}, 'Under 1'),
createElement('option', {
attrs: {
value: 1
}
}, '1'),
]
)
},
})

You can see the working fiddle here: https://jsfiddle.net/aaoehLqe/1/


To understand how it works, you can see the API details of createElement in docs:


// @returns {VNode}
createElement(
// {String | Object | Function}
// An HTML tag name, component options, or function
// returning one of these. Required.
'div',
// {Object}
// A data object corresponding to the attributes
// you would use in a template. Optional.
{
// (see details in the next section below)
},
// {String | Array}
// Children VNodes. Optional.
[
createElement('h1', 'hello world'),
createElement(MyComponent, {
props: {
someProp: 'foo'
}
}),
'bar'
]
)

[#58588] Thursday, March 9, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brianaclaras

Total Points: 23
Total Questions: 106
Total Answers: 111

Location: Japan
Member since Sat, Jun 6, 2020
4 Years ago
brianaclaras questions
Fri, Oct 15, 21, 00:00, 3 Years ago
Thu, Dec 3, 20, 00:00, 4 Years ago
Mon, May 25, 20, 00:00, 4 Years ago
Wed, Mar 4, 20, 00:00, 4 Years ago
;