代码之家  ›  专栏  ›  技术社区  ›  Andrew Li

如何处理Vuejs中V-for组件发出的事件

  •  2
  • Andrew Li  · 技术社区  · 7 年前

    我正在尝试处理由 v-for .
    例如, 我做了一个 combobox 更改值时发出事件的组件。
    它通过 this.$emit('item_change', item); .
    我要为对应的用户处理此事件。
    在下面的代码中,我想更改 status 更改用户的值时用户的值 下拉列表框 属于 user .
    它得到 item 当使用时作为参数 v-on:item_change="status_change"
    example
    但它没有 项目 作为参数 v-on:item_change="status_change(item , user)" 虽然 下拉列表框 项目 地位 属于 用户 保留原始值。

    我如何解决这个问题?

    JSFiddle Example

    <div id="mainapp">
     <table>
      <thead>
        <th>Name</th><th>Status</th>
      </thead>
      <tbody>
        <tr v-for="user in users">
          <td>{{user.name}}</td>
          <td><combobox v-bind:default="user.status" v-bind:data="status_codes" v-on:item_change="status_change(item, user)"></combobox></td>
        </tr>
      </tbody>
      </table>
    </div>
    

    JS码

    var combobox = Vue.component('combobox', {
      data: function () {
        return {
          selected_item:{title:'Select', value:-1},
          visible:false
        }
      },
      props:['data','default','symbol'],
      template: `
        <div class="combobox">
          <span class="symbol" v-if="!symbol">
            <i class="fa fa-chevron-down" aria-hidden="true"  ></i>
          </span>
          <span class="main" v-on:click="toggleVisible">{{selected_item.title}}</span>
          <ul class="combodata" v-if="visible">
            <li class="item" v-for="item in data" v-on:click="select(item)">{{item.title}}</li>
          </ul>
        </div>
      `,
      created:function(){
        if(this.data.length>0){
          if(this.default == null || this.default == undefined || this.default =='') this.default=0;
          this.selected_item = this.data[this.default];
        }
      },
      methods:{
        toggleVisible:function(){
          this.visible = !this.visible;
        },
        select:function(item){
          if(this.selected_item != item){
            this.selected_item= item;
            this.$emit('item_change', item);
          }
          this.visible = false;
        }
      }
    });
    
    var app=new Vue({
      el:"#mainapp",
      data:{
         status_codes:[{title:'Inactive', value:0},{title:'Active', value:1}],
         users:[{name:'Andrew', status:1},{name:'Jackson', status:0},{name:'Tom', status:1}]
      },
      methods:{
         status_change:function(item,user){  //This gets only the parameter from the event. How could I pass the additional parameters to this function?
            console.log(item,user);
            try{
              user.status = item.value;
            }catch(e){ console.log}
         }
      }
    });
    
    3 回复  |  直到 7 年前
        1
  •  0
  •   Husam Ibrahim    7 年前

    你需要通过 $event 对你 status_change 处理程序而不是 item

    <div id="mainapp">
        <table>
            <thead>
                <th>Name</th>
                <th>Status</th>
            </thead>
            <tbody>
                <tr v-for="user in users">
                    <td>{{user.name}}</td>
                    <td>
                        <combobox v-bind:default="user.status" v-bind:data="status_codes" v-on:item_change="status_change($event, user)"></combobox>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    

    JSFiddle

    See the Vue docs here about event handling

    有时我们还需要访问内联语句处理程序中的原始DOM事件。可以使用特殊的$event变量将其传递到方法中

        2
  •  2
  •   Jacob Goh    7 年前

    使用 $event

    你真正需要的是 v-on:item_change="status_change($event , user)" .

    当你这样做的时候 this.$emit('item_change', whatever); , whatever 将成为 美元事件 在事件侦听器中。

    https://jsfiddle.net/jacobgoh101/bLsw085r/1/

        3
  •  1
  •   andrunix    7 年前

    尝试向函数传递参数,如下所示:

    v-on:item_change="status_change(item, user)"
    

    在函数声明中,指定参数:

    status_change: function (item, user) {
       console.log(item, user);
    }
    
    推荐文章