这是一个简单的vue演示:
<body>
<div id="app">
<ul>
<li v-for="text,i in todoList" v-on:click="method_1(i)" >{{text}}</li>
</ul>
<ul>
<li v-for="text,i in todoList" v-on:click="method_2" >{{text}}</li>
</ul>
<ul>
</ul>
</div>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>
<script>
new Vue({
el:"#app",
data:{
todoList:["aaaaa",'bbbbbb','ccccc']
},
methods:{
method_1:function(i){
console.log(i) // i is index of v-for
},
method_2:function(e){
console.log(e)
},
}
})
</script>
</body>
Run Code Online (Sandbox Code Playgroud)
我想绑定onclick函数并把event它index from v-for作为这个函数的参数.
如果我使用v-on:click="method_1(i)":
- 这将i是以前的变量;
如果我使用eles v-on:click="method_1:
- vue将自动采用click eventparams.
但是我想要click event以前的两个变量作为我的函数的参数.我怎么能这样做?
Ru *_*ong 11
要同时使用事件和参数,你需要有类似的东西 example($event, params)
用你的例子,
<li v-for="(text, i) in todoList" v-on:click="method_1($event, i)">
{{ text }}
</li>
Run Code Online (Sandbox Code Playgroud)
methods: {
method_1: function(ev, i){
console.log(ev) // this is the event
console.log(i) // i is index of v-for
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4890 次 |
| 最近记录: |