admin管理员组文章数量:1410712
I want using Vue.js Render function to make ponent in javascript.Now I can make a HTML structure one SPAN and one BUTTON.when I click the button,I expect it output in console,but it just not work.here is my code :
<script src=".js"></script>
<div id="app">
<counter></counter>
</div>
<script>
var a = {
data () {
return {count: 1}
},
methods: {
inc () {console.log(this.count)}
},
render:function(h){
var self = this
var buttonAttrs ={
on:{click:self.inc}
}
var span = h('span',this.count.toString(),{},[])
var button = h('button','+',buttonAttrs,[])
return h('div'
,{},
[
span,
button
])
}
}
new Vue({
el:'#app',
ponents:{
counter : a
}}
)
</script>
or on codepen Any response is wele and thank you .
I want using Vue.js Render function to make ponent in javascript.Now I can make a HTML structure one SPAN and one BUTTON.when I click the button,I expect it output in console,but it just not work.here is my code :
<script src="https://unpkg./vue/dist/vue.js"></script>
<div id="app">
<counter></counter>
</div>
<script>
var a = {
data () {
return {count: 1}
},
methods: {
inc () {console.log(this.count)}
},
render:function(h){
var self = this
var buttonAttrs ={
on:{click:self.inc}
}
var span = h('span',this.count.toString(),{},[])
var button = h('button','+',buttonAttrs,[])
return h('div'
,{},
[
span,
button
])
}
}
new Vue({
el:'#app',
ponents:{
counter : a
}}
)
</script>
or on codepen Any response is wele and thank you .
Share Improve this question asked Nov 24, 2016 at 5:15 recoreco 3053 silver badges10 bronze badges1 Answer
Reset to default 7Your use of the createElement
method is incorrect when building your button, since you are passing the wrong series of arguments.
First off, you should set the inner html +
via your button attributes object, not via the second argument which is reserved for the data object, per the documentation:
// {Object} // A data object corresponding to the attributes // you would use in a template. Optional. { // (see details in the next section below) },
As such, you should structure your buttonsAttrs object as follows:
var buttonAttrs = {
on: { click: self.inc },
domProps: {
innerHTML: '+'
},
};
Second, you should pass the buttonAttrs as the second argument in your createElement
call per the above documentation:
var button = h('button', buttonAttrs, []);
See this working codepen.
本文标签: javascriptvuejs add event listener to button in Render functionStack Overflow
版权声明:本文标题:javascript - vue.js add event listener to button in Render function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744267893a2598036.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论