**(1):defineProps:**传入要使用的props定义自定义属性,传递过来的值具有响应式,和props一样;
**(2):defineEimts:**传入要自定义的事件,emit实例去传入自定义事件的值,和$emit或context.emit一样;
父组件:
javascript
<template>
<initInput :index="8" @changeInit="changeInit"></initInput>
</template>
<script lang="ts" setup>
import initInput from './computed/initInput.vue';
const changeInit = (e: any) => {
console.log(e); // 100
}
</script>
子组件:
javascript
<template>
<div>
<el-button type="primary" @click="change">点击</el-button>
</div>
</template>
<script lang="ts" setup>
import { onMounted, getCurrentInstance } from 'vue';
import { useRoute, useRouter } from 'vue-router';
// data数据
let { proxy: this_ }: any = getCurrentInstance();
let route = useRoute();
let router = useRouter();
// mounted
// methods方法
let props = defineProps({
index: {
type: Number,
default: 0
}
});
let emit = defineEmits(["changeInit"]);
const change = () => {
console.log(props.index); // 8
emit("changeInit", 100);
}
</script>
<style scoped lang='scss'>
</style>