2

我正在尝试将参数传递给Vue.js中的@select事件函数

HTML

 <template>
    <div class="container">

<div v-for="(billItem, k) in billItems" :key="k" >
    <div class="form-group row">
        <label class="col-form-label col-sm-3" for=""> Products</label>
        <div class="col-sm-3">
        <div class="form-group">
            <label for="">Product</label>
            <multiselect 
                v-model="billItem.billItem_selectedGood" 
                :options="productOptions" 
                :close-on-select="true" 
                :clear-on-select="false" 
                :hide-selected="true" 
                :preserve-search="true" 
                placeholder="Select Product" 
                label="name" 
                track-by="name" 
                :preselect-first="false"
                id="example"
                @select="onSelect_selectedGood"
            >
            </multiselect>
        </div>
    </div>
</div>

</div>
</template>

JS

<script>

export default {
  data(){
    return {
      form: new Form({
      })
    }
  },
  methods : {
    onSelect_selectedGood( option, id) {
      console.log("onSelect_selectedGood");
      console.log(option);
    }
  },
  mounted() {
      
  }
}
</script>

我的问题:如何将billItem传递给onSelect_selectedGood以便我可以在函数内部访问它。

然后像 @select="onSelect_selectedGood(billItem)"这样实现这样的功能onSelect_selectedGood( billItem, option, id)

让我知道如何实现它。

4

2 回答 2

5

你可以这样做:

 @select="onSelect_selectedGood($event,billItem)"

并在您的方法中:

 methods : {
   onSelect_selectedGood( selectedOption,billItem) {
      console.log(  selectedOption,billItem);

   },
}

传递的参数$eventselectedOptionand billItem

于 2019-05-06T20:58:45.797 回答
0

如果你想访问所有的billItem, optionand id,你可以创建一个自定义的输入组件:

自定义输入.vue

<template>
  <multiselect 
      v-model="billItem.billItem_selectedGood" 
      :options="productOptions" 
      :close-on-select="true" 
      :clear-on-select="false" 
      :hide-selected="true" 
      :preserve-search="true" 
      placeholder="Select Product" 
      label="name" 
      track-by="name" 
      :preselect-first="false"
      id="example"
      @select="onSelect_selectedGood"
   >
</multiselect>
</template>

<script>
export default {
  props: ['billItem'],
  methods: {
    onSelect_selectedGood( option, id) {
      console.log("onSelect_selectedGood");
      console.log(option);
      console.log(this.billItem)
    }
  }
}
</script>

然后在你的 html 中使用它:

<custom-input 
  :billItem="billItem"
/>

billItem因为您作为道具传递,所以您可以this.billItem在自定义组件中访问它。

于 2019-05-06T21:03:26.287 回答