Vue3+TypeScript父子组件传值

简介: 将数据从父组件传递到子组件(Props)

将数据从父组件传递到子组件(Props)

子组件


我们先来创建一个子组件


<template>
  <div class="child">
    <h3>{{title}}</h3>
    <p>{{content}}</p>
  </div>
</template>
<script setup lang="ts">
  //从父级接收数据,定义接口
  interface Props {
    title: string;
    content: string
  }
  //defineProps() 加上定义的接口名称
 // defineProps<Props>();
const propsData = defineProps<Props>();
  console.log('propsData',propsData.title,propsData.content)
</script>

以上代码,我们从父级接收到数据,并定义了数据接口,然后使用defineProps() 加上定义的接口名称。也可以定义一个propsData参数来打印出数据。


父组件


<template>
  <child-comp title="学习vue3" :content="content" />
</template>
<script setup>
import ChildComp from "../../components/ChildComp.vue";
import {ref} from "vue";
const content = ref('一起来学习vue3吧')
</script>

将数据从子组件传递到父组件(Emit)

子组件


<template>
  <div class="child">
    <h3>{{title}}</h3>
    <p>{{content}}</p>
    <button @click="buttonClick">点击获取数据</button>
  </div>
</template>
<script setup lang="ts">
  //从父级接收数据,定义接口
  interface Props {
    title: string;
    content: string
  }
  //defineProps() 加上定义的接口名称
  const propsData = defineProps<Props>();
  console.log('propsData',propsData.title,propsData.content)
  //给传给父组件的数据定义一个接口
  interface Emits {
    (event:"sendMessage",msg:msg) : void;
  }
  const emit = defineEmits<Emits>();
  //定义一个msg的数据
  const msg = '这是从子组件传到父组件的数据'
  const buttonClick = () => {
    emit("sendMessage",msg)
  }
</script>


父组件


<template>
  <child-comp title="学习vue3" :content="content"  @sendMessage="getMessage"/>
</template>
<script setup>
import ChildComp from "../../components/ChildComp.vue";
import {ref} from "vue";
const content = ref('一起来学习vue3吧')
//在父组件接收子组件传过来的数据
const getMessage = (msg) => {
  console.log('msg',msg)
}
</script>


相关文章
|
18天前
|
JavaScript 前端开发 数据可视化
前端开发使用 Vue 3 + TypeScript + Pinia + Element Plus + ECharts
前端开发使用 Vue 3 + TypeScript + Pinia + Element Plus + ECharts
54 0
|
18天前
|
JavaScript 前端开发
在Vue中使用TypeScript的常见问题有哪些?
在Vue中使用TypeScript的常见问题有哪些?
37 2
|
18天前
|
JavaScript 前端开发
在Vue中使用TypeScript的优缺点是什么?
在Vue中使用TypeScript的优缺点是什么?
22 0
|
18天前
|
JavaScript
在 Vue 中如何使用 TypeScript?
在 Vue 中如何使用 TypeScript?
17 0
|
18天前
|
JavaScript 安全 容器
Vue3 + setup + TypeScript: 构建现代、类型安全的Vue应用的关键技巧总结
当使用 setup 的时候,组件直接引入就可以了,不需要再自己手动注册
|
18天前
|
JavaScript Go 数据库
用Typescript 的方式封装Vue3的表单绑定,支持防抖等功能。
Vue3 的父子组件传值、绑定表单数据、UI库的二次封装、防抖等,想来大家都很熟悉了,本篇介绍一种使用 Typescript 的方式进行统一的封装的方法。
|
18天前
|
前端开发 JavaScript 测试技术
Vue3+Vite+TypeScript常用项目模块详解(下)
现在无论gitee还是github,越来越多的前端开源项目采用Vue3+Vite+TypeScript+Pinia+Elementplus+axios+Sass(css预编译语言等),其中还有各种项目配置比如eslint 校验代码工具配置等等,而我们想要进行前端项目的二次开发,就必须了解会使用这些东西,所以作者写了这篇文章进行简单的介绍。
|
18天前
|
JavaScript 前端开发 API
Vue3+Vite+TypeScript常用项目模块详解
现在无论gitee还是github,越来越多的前端开源项目采用Vue3+Vite+TypeScript+Pinia+Elementplus+axios+Sass(css预编译语言等),其中还有各种项目配置比如eslint 校验代码工具配置等等,而我们想要进行前端项目的二次开发,就必须了解会使用这些东西,所以作者写了这篇文章进行简单的介绍。
Vue3+Vite+TypeScript常用项目模块详解
|
18天前
|
JavaScript
Vue3 + Typescript + Node.js 搭建elementUI使用环境
Vue3 + Typescript + Node.js 搭建elementUI使用环境
48 0
|
JavaScript 前端开发
基于Vue2+TypeScript的项目规划搭建
最近手头的项目都已经接近尾声,时间比较宽裕,于是想着升级一下网站。上一版的网站还是我刚接触前端时设计的,使用Zepto为主要框架,代码毫无模块化思想,因为后续的功能越加越多,现在每次维护都有自杀的冲动。
1594 0