vue.js - 如何在子组件中使用父组件传递的函数
问题描述
我有一个叫孩子的父母:
<select-school-type placeholder="Filter par type école" @selected="getSchools"></select-school-type>
我希望在用户更改子组件中的值时调用“getSchools”方法。getSchool 方法使用所选值调用端点。
我的孩子是:
<template>
<select @change="onChange($event)">
<option>{{ placeholder }}</option>
<option v-for="schoolType in schoolTypes" :key="schoolType.id">{{ schoolType.id }}</option>
</select>
</template>
在我的方法中,我有这个:
onChange(event){
console.log(event.target.value)
this.$emit('selected')
}
我看到带有正确值的 console.log 消息。
之后,如何使用正确的值调用 getSchools 方法?我尝试了很多事情都没有成功。
我是 Vue 初学者。
解决方案
您需要将值传递给$emit
方法:
this.$emit('selected', event.target.value)
推荐阅读
- django - Django modelform字段如何禁用和防止篡改?
- javascript - Django - 无法访问根文件夹下的静态 css 文件。控制台中的错误 404
- rest - 测试我是否通过 TLS1.2 进行通信的 Rest 服务
- javascript - 根据上一个下拉菜单中的选定值填充下拉菜单?
- android - 带有 ConstraintLayout 的自定义 DialogFragment
- javascript - 如何使用javascript将值转换为数组
- python - 无法从 PyCharm 导入 TensorFlow
- r - 避免重新排序和分组重复的 x 值 ggplot (geom_point)
- python - 熊猫面板已弃用,
- reactjs - Reselect 相对于容器组件的优势