【问题标题】:Attaching @click to array element将@click 附加到数组元素
【发布时间】:2018-05-03 11:22:09
【问题描述】:

我想将@click="method" 添加到数组内的指定元素中。元素显示为li,使用v-for='item in menuItems'

menuItems = [
  { title: 'test1', route: '/test1'}
  { title: 'test2', route: '/test2}
]

这是我尝试做的:

  this.menuItems.forEach((item) => {
     if(item.route === '/test1') {
      item.attr('@click', 'method')
     }
  })

【问题讨论】:

    标签: javascript vue.js


    【解决方案1】:

    试试这个方法

    <li v-for="item in menuItems" @click="method(item)">
    
    method: function(item) { 
        console.log(item.title);
    }
    

    完整示例: https://jsfiddle.net/mgzyufq8/

    【讨论】:

    • 我需要添加@click.native="",现在可以了。谢谢。
    猜你喜欢
    • 2021-07-04
    • 2018-08-18
    • 2014-02-24
    • 2020-07-07
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    相关资源
    最近更新 更多