只有一个形参如何传两个参数

2021-11-11  本文已影响0人  14px

比如有个组件叫 xxx,它支持传递一个叫 @change 的事件,这个事件中会自带一个形参,它能打印出组件名称。

<template>
  <xxx @change="query" />
</template>
<script>
export default {
  methods: {
    query(x) {
      console.log(x) // xxx
    }
  }
}
</script>

但某些时候它处于一个 for 循环中,我需要知道它的 index,于是都传进去,结果报错了,直接就是:x is not defined

<template>
  <xxx @change="query(x, index)" for="(item,index) in list" :key="index" />
</template>
<script>
export default {
  methods: {
    query(x, index) {
      console.log(x)
      console.log(index)
    }
  }
}
</script>

应该这么传才对:

<template>
  <xxx @change="x => { query(x, index) }" for="(item,index) in list" :key="index" /> 
</template>
<script>
export default {
  methods: {
    query(x, index) {
      console.log(x) // xxx
      console.log(index) // 2
    }
  }
}
</script>
上一篇 下一篇

猜你喜欢

热点阅读