当前位置: 首页 > news >正文

简洁网站首页模板5月疫情最新消息

简洁网站首页模板,5月疫情最新消息,深圳华强北网站建设,建筑方案的网站在 Vue 3 中,父子组件之间的数据传递是一个常见的需求。父组件可以通过 props 将数据传递给子组件,而子组件可以通过 defineProps 接收这些数据。本文将详细介绍父子组件传值的使用方法,并通过优化后的代码示例演示如何实现。 1. 父子组件传值…

在 Vue 3 中,父子组件之间的数据传递是一个常见的需求。父组件可以通过 props 将数据传递给子组件,而子组件可以通过 defineProps 接收这些数据。本文将详细介绍父子组件传值的使用方法,并通过优化后的代码示例演示如何实现。


1. 父子组件传值的基本概念

1.1 Props 的作用

  • Props 是父组件向子组件传递数据的一种方式。
  • 子组件通过 defineProps 接收父组件传递的数据。

1.2 单向数据流

  • 数据从父组件流向子组件,子组件不能直接修改父组件传递的数据。
  • 如果需要修改父组件的数据,可以通过 事件 通知父组件。

2. 父组件向子组件传递数据

2.1 父组件代码

<template><Person :title="title" :list="persons" />
</template><script lang="ts" setup name="App">
import Person from "./components/Person.vue";
import { reactive } from "vue";
import { type Persons } from "@/types";// 定义响应式数据
const title = "人员列表";
const persons = reactive<Persons>([{ id: "1", name: "John", age: 20 },{ id: "2", name: "Jane", age: 21 },{ id: "3", name: "Jim", age: 22 },
]);
</script><style>
/* 全局样式 */
</style>

2.2 代码解析

  1. 传递数据

    • 父组件通过 :title="title"title 字符串传递给子组件。
    • 通过 :list="persons"persons 数组传递给子组件。
  2. 响应式数据

    • 使用 reactive 创建响应式数组 persons

3. 子组件接收数据

3.1 子组件代码

<template><div class="person"><h1>{{ title }}</h1><ul><li v-for="item in list" :key="item.id">{{ item.name }} - {{ item.age }} 岁</li></ul></div>
</template><script setup lang="ts">
import { type Persons } from "@/types";// 接收 props
defineProps<{ title: string; list: Persons }>();
</script><style scoped>
.person {background-color: skyblue;box-shadow: 0 0 10px;border-radius: 10px;padding: 20px;
}h1 {font-size: 24px;margin-bottom: 20px;
}ul {list-style-type: none;padding: 0;
}li {margin: 10px 0;font-size: 18px;
}
</style>

3.2 代码解析

  1. 接收数据

    • 使用 defineProps 接收父组件传递的 titlelist
    • 通过泛型 <{ title: string; list: Persons }> 定义 props 的类型。
  2. 渲染数据

    • 在模板中使用 {{ title }} 显示标题。
    • 使用 v-for 遍历 list 并渲染每个人员的姓名和年龄。

4. 使用 withDefaults 设置默认值

如果父组件没有传递某些 props,我们可以使用 withDefaults 为 props 设置默认值。

4.1 子组件代码(带默认值)

<script setup lang="ts">
import { type Persons } from "@/types";// 接收 props 并设置默认值
withDefaults(defineProps<{ title?: string; list?: Persons }>(), {title: "默认标题",list: () => [{ id: "default-1", name: "默认人员1", age: 18 },{ id: "default-2", name: "默认人员2", age: 19 },],
});
</script>

4.2 代码解析

  1. 设置默认值
    • 使用 withDefaultstitlelist 设置默认值。
    • 如果父组件没有传递 titlelist,子组件将使用默认值。

5. 完整代码示例

5.1 父组件(App.vue)

<template><Person :title="title" :list="persons" />
</template><script lang="ts" setup name="App">
import Person from "./components/Person.vue";
import { reactive } from "vue";
import { type Persons } from "@/types";// 定义响应式数据
const title = "人员列表";
const persons = reactive<Persons>([{ id: "1", name: "John", age: 20 },{ id: "2", name: "Jane", age: 21 },{ id: "3", name: "Jim", age: 22 },
]);
</script><style>
/* 全局样式 */
</style>

5.2 子组件(Person.vue)

<template><div class="person"><h1>{{ title }}</h1><ul><li v-for="item in list" :key="item.id">{{ item.name }} - {{ item.age }} 岁</li></ul></div>
</template><script setup lang="ts">
import { type Persons } from "@/types";// 接收 props 并设置默认值
withDefaults(defineProps<{ title?: string; list?: Persons }>(), {title: "默认标题",list: () => [{ id: "default-1", name: "默认人员1", age: 18 },{ id: "default-2", name: "默认人员2", age: 19 },],
});
</script><style scoped>
.person {background-color: skyblue;box-shadow: 0 0 10px;border-radius: 10px;padding: 20px;
}h1 {font-size: 24px;margin-bottom: 20px;
}ul {list-style-type: none;padding: 0;
}li {margin: 10px 0;font-size: 18px;
}
</style>

6. 总结

  • 父子组件传值

    • 父组件通过 props 向子组件传递数据。
    • 子组件通过 defineProps 接收数据。
  • 默认值

    • 使用 withDefaults 为 props 设置默认值。
  • 单向数据流

    • 数据从父组件流向子组件,子组件不能直接修改父组件的数据。

通过本文的介绍和优化后的代码示例,希望你能更好地理解 Vue 3 中父子组件传值的使用方法,并在实际项目中灵活运用!

http://www.dt0577.cn/news/7243.html

相关文章:

  • wordpress运行php代码seo分析网站
  • 哪些网站做兼职可靠吗制作网站要多少费用
  • 网站建设行业市场分析百度推广费用报价单
  • 网站空间提供企业管理培训课程费用
  • 网站建设公司首选淘宝关键词搜索排名
  • 网上做公益的网站免费网站推广群发软件
  • 深圳最新疫情风险等级地区名单旺道seo软件
  • 沈阳网站建设咨询郑州网络营销公司哪家好
  • 小程序开发公司小程序开发公司快速网站推广优化
  • 手机网站建设做竞价推广的技巧sem是什么意思啊
  • 广州网站开发哪家公司好申请网站域名要多少钱
  • 东莞网站建设 拉伸膜手机最新产品新闻
  • 做网站需要几个服务器seo优化技术厂家
  • 如何看一个网站做的如何每天看七个广告赚40元的app
  • 网站上删除信息如何做省好多会员app
  • 化妆品网站建设方案常见的推广方式
  • 网站使用培训方案seo建站教程
  • 邯郸网站建设优化推广网站淄博
  • 商城开发网站建设百度宁波运营中心
  • 女生做网站前端设计师网络营销主要特点有哪些
  • 儿童玩具网站模板郑州网络推广排名
  • 网站开放培训微信scrm
  • 广州专业做网站排名哪家好网站策划方案范文
  • W做网站济南优化seo公司
  • 支付网站建设费用计入关键词排名优化品牌
  • 17zwd一起做网站普宁上海推广系统
  • 做网站能用的字体seo搜索引擎优化工资
  • seo免费网站建设网站描述和关键词怎么写
  • 做网站一个月20g流量够吗外贸seo推广公司
  • 免费发布信息的网站百度下载应用