微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Vuejs中将功能作为prop传递不起作用

如何解决在Vuejs中将功能作为prop传递不起作用

我试图创建一个自定义选择器,该选择器我会多次重复使用,并且我想为选择时必须触发的功能设置一个prop。这是我的代码(仅包含其中的相关部分):

主选择器:

<template>
 <div>
    <select name="selector" >
        <option :key="option" v-for="option in options" @change="selection"> {{option}} </option>
    </select>
 </div>
</template>

<script>
export default {
  name: 'CoffeeSelector',props: {
    options: Array,selection: Function
  },}
</script>

我使用它的页面

<template>
  <div>
    <custom-selector :options='types' :selection="coucou"> </custom-selector>
  </div>
</template>

<script>
import CustomSelector from './CustomSelector.vue'

export default {
  name: 'Selectors',components: {
    CustomSelector
  },methods: {
    coucou(){
      console.log("coucou")
    }
  }
}
</script>

执行此操作时,更改选择无任何反应。关于它为什么发生的任何想法?预先感谢

解决方法

您可以向父组件发出事件。

<template>
 <div>
    <select name="selector" >
        <option :key="option" v-for="option in options" @change="onOptionChange(option)"> {{option}} </option>
    </select>
 </div>
</template>

<script>
export default {
  name: 'CoffeeSelector',props: {
    options: Array
  },methods: {
    onOptionChange(option) {
      this.$emit('selection',option);
    }
  }
}
</script>

然后,您可以在父组件中监听这些事件。

<template>
  <div>
    <custom-selector :options='types' v-on:selection="coucou"> </custom-selector>
  </div>
</template>

<script>
import CustomSelector from './CustomSelector.vue'

export default {
  name: 'Selectors',components: {
    CustomSelector
  },methods: {
    coucou(option){
      console.log("coucou: " + option)
    }
  }
}
</script>
,

尝试将您的re-queue移至@change标记

select

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。