[vue2 event parameter passing 1] Custom parameters: In the change event of elementui, the method of passing custom parameters

1. Question:

        In the development process of elementUI, sometimes we often encounter that we want to pass some customized data in the defined change event. How should we solve this problem?

2. Solution:

1. Change event of select tag

                Normal situation : The currently selected value is returned by default.

                Special case : What if you want to pass the custom parameter index? You need to use $event to get the default value, and then pass the custom parameters later.

                Note: [The $event refers to what event is currently triggered (mouse event, keyboard event, etc.)]

2. Change event in InputNumber counter

                Normal situation: NewValue and oldValue are returned by default.

                Special case: What if you want to pass the custom parameter index? You need to use arrow functions to work around it and realize the transfer of custom parameters.

3. Specific cases:

Case 1. Change event of select tag

        【Prepare data】

         Define the data in the select tag options in data as the options array. The data format is as follows:

data() {
      return {
        options: [{
          value: '选项1',
          label: '黄金糕'
        }, {
          value: '选项2',
          label: '双皮奶'
        }, {
          value: '选项3',
          label: '蚵仔煎'
        }, {
          value: '选项4',
          label: '龙须面'
        }, {
          value: '选项5',
          label: '北京烤鸭'
        }],
        value: ''
      }

        【normal circumstances】

        Use the select tag in the <template> template to display the drop-down options, and bind the change event to selectedItem. code show as below:

<el-select v-model="value" placeholder="请选择" @change="selectedItem">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
</el-select>

        In the methods method, define the selectedItem method to respond to the label change event. code show as below:

    selectedItem(e){
      console.log("你选择了:",e)
    },

        The page effect is:

2a9642c7ee5b4d9087369b8f49c15743.png

 Of course, you can also pass it directly in object form. The specific changes to the code are as follows:

db037577e97540909394ffa19e87f955.png

         【Special cases】

        Pass custom parameters. Taking the above data as an example, if you want to pass a custom index value at the same time, how should you do it?

        1. Change the writing method of the binding event in the label event:

3d7bc8e5e5a341278934c89602b1c056.png

         2. Change the number of parameters passed in the binding event:

a4a9e4f4da964e299dcb97079b29650e.png

         The output is:

34e49be9040c424db496b891d55b8f21.png

 

<>Case 2, change event in InputNumber counter

        【normal circumstances】

34bfd6dea0484faab80cf400d8c29838.png

         Page effect:

9f42fccbbf0840b0a28ef885f8639110.png

        【Special cases】

b7be148e91f14b5abfc29b3162a9b911.png

 Page effect:

ed41930211034ef3b8d1b61275a3421a.png

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/m0_60318025/article/details/129908410