<html>
<head>
<script src='./vue.js'></script>
</head>
<body>
<div id='root'>
<input v-model='msg'/>
<button @click='handleSubmit'>Submit</button>
<ul>
<todoitem v-for='(item,index) of list' :key='index' :content='item'></todoitem>
</ul>
</div>
<script>
Vue.component('todoitem',{
props:['content'],
template:'<li v-bind:click='handleClick'>{{content}}</li>',
methods:{
handleClick:function(){
alert('clicked')
}
}
})
new Vue({
el:'#root',
data:{
msg:'',
list:[]
},
methods:{
handleSubmit:function(){
this.list.push(this.msg)
this.msg=''
}
}
})
</script>
</body>
</html>
你这个问题是Js中单引号里面不能嵌套单引号,双引号里面可以嵌套单引号的,改成这样就行template: "<li v-bind:click='handleClick '>{{content}}</li>"
如下图:
绑定点击事件不是 v-on:click 或者@:click 么,你写的v-bind:click 也可以的么?