vite+ts+vue3 知识点(父子组件传参)

父传子 

如果你使用的是JS

父组件(js)

<template>
  <div>
    <div>我是父组件</div>
    <hr />
    <Son :data="data"></Son>
  </div>
</template>

<script setup lang="ts">
import Son from "@/components/son.vue";
const data = { name: "李子天" };
</script>

<style scoped></style>

子组件(js)

<template>
  <div>子组件 {{ data.name }}</div>
</template>

<script setup lang="ts">
const prop = defineProps({
  data: {
    type: Object,
    default: { name: "梁延涛" },
  },
});
console.log(prop.data);
</script>

<style scoped></style>

如果你使用的是TS

父组件(ts)

<template>
  <div>
    <div>我是父组件</div>
    <hr />
    <Son :data="data" :arr="[1, 2, 3]"></Son>
  </div>
</template>

<script setup lang="ts">
import Son from "@/components/son.vue";
let data = { name: "李子天" };
</script>

<style scoped></style>

子组件(ts)

<template>
  <div>子组件 {{ data.name }} {{ arr }}</div>
</template>

<script setup lang="ts">
const props = withDefaults(
  // 接收父组件传来的数据
  defineProps<{
    data: {
      name: string;
    };
    arr: number[];
  }>(),
  // 设置默认值
  {
    data: () => {
      return { name: "lala" };
    },
    arr: () => [0],
  }
);
console.log(props);
</script>

<style scoped></style>

子传父

如果你使用的是JS

子组件(js)

<template>
  <button @click="btn">子传父</button>
</template>

<script setup lang="ts">
// 给父组件传值
const emit = defineEmits(["toFatherClick"]);
const btn = () => {
  emit("toFatherClick", { age: 80 });
};
</script>

<style scoped></style>

父组件(js)

<template>
  <div>
    <div>我是父组件</div>
    <hr />
    <Son @toFatherClick="getAge"></Son>
  </div>
</template>

<script setup lang="ts">
import Son from "@/components/son.vue";
const getAge = (data) => {
  console.log(data);
};
</script>

<style scoped></style>

如果你使用的是TS

子组件(ts)

<template>
  <div>子组件 {{ data.name }} {{ arr }}</div>
  <button @click="btn">子传父</button>
</template>

<script setup lang="ts">
// 给父组件传值
const emit = defineEmits<{
  (e: "toFatherClick", arr: number[]): void;
}>();
const btn = () => {
  emit("toFatherClick", [1]);
};
console.log(props);
</script>

<style scoped></style>

父组件(ts)

<template>
  <div>
    <div>我是父组件</div>
    <hr />
    <Son @toFatherClick="getArr"></Son>
  </div>
</template>

<script setup lang="ts">
import Son from "@/components/son.vue";
const getArr = (arr: number[]) => {
  console.log(arr);
};
</script>

<style scoped></style>

暴露方法

子组件

<template>
  <div>子组件</div>
</template>

<script setup lang="ts">
// 暴露出去
defineExpose({
  name: "老天师",
  open: () => console.log(1)
});
</script>

<style scoped>

</style>

父组件

<template>
  <div>
    <div>我是父组件</div>
    <hr />
    <sonTest ref="mySon"></sonTest>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import sonTest from "@/components/son.vue";
const mySon = ref();

// 父组件要拿到子组件中暴露的数据必须在onMounted中获取
onMounted(() => {
  console.log(mySon.value?.name); // 老天师
  mySon.value?.open() // 1
})
</script>

<style scoped>

</style>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微光无限

破晓的日子还有很多!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值