父调用子的方法 父:
<template>
<div @click="handle">调用子组件的方法</div>
<child ref="alarmDetailRef" />
</template>
<script setup>
import { ref } from "vue";
let alarmDetailRef = ref(null);
function handle() {
alarmDetailRef.value.childFn();
}
</script>
子:
<script setup>
function childFn() {
}
defineExpose({
childFn,
});
</script>
父给子传参 父:
<template>
<div @click="handle">调用子组件的方法</div>
<child ref="alarmDetailRef" :a="a" />
</template>
<script setup>
import { ref } from "vue";
let a = ref('父传子')
</script>
子:
<template>
<div>{{a}}</div>
</template>
<script setup>
import { ref } from "vue";
defineProps({
a: {
default: "",
type: String,
},
});
</script>
子传父 父:
<template>
<div>子传父</div>
<child @fn="fn" />
</template>
<script setup>
import { ref } from "vue";
let a = ref('')
function fn(e){
a.value = "子触发父事件"
console.log(e);
}
</script>
子:
<template>
<el-button @click="ff">子传父</el-button>
</template>
<script setup>
import { ref } from "vue";
const emit = defineEmits(["fn"])
function ff(){
emit("fn",true)
}
</script>
|