父子传参
子组件 Child.vue
<template>
<div>
<button @click="childEmit">父组件传参</button>
</div>
</template>
<script>
export default {
setup(props,{emit}){
function childEmit () {
emit('my-emit', '我是子组件值')
}
return{
childEmit
}
}
};
</script>
父组件 father.vue
<template>
<div>
<child @my-emit="parentEmit"></child>
</div>
</template>
<script>
import Child from "./Child.vue";
import { ref } from "vue";
export default {
components: {
Child,
},
setup() {
function parentEmit(val){
}
return{
parentEmit
}
},
};
</script>
案例
搜索:【一级搜索,二级搜索(高级搜索)】
const emit = defineEmits(['heightSearch','firstSearch'])
const searchWord = ref<string>('')
const wordSearch = ()=>{
emit('firstSearch',searchWord.value)
}
const goSearch = ()=>{
showLables.business_scope = form.business_scope
let contact_str = '',ctype_str = '';
form.contact.forEach(v=>{
contact_str += contactArr.value.find(j=>j.value == v)?.name + ','
})
...
emit('heightSearch',getSearchParams())
}
const getSearchParams()= ()=>{
let searchParams = {}
let company_type = 0
let contact = 0
...}
|