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

Vue3 + TypeScript,使用祖先传后代模式重构父传子模式

父传子模式

父组件 SampleInput.vue

<script setup lang="ts" name="SampleInput">
import { ref } from "vue";
import type { ApplyBasicInfo, Apply, ApplySample } from "@/interface";
import CommonApplySampleTable from "@/components/common/CommonApplySampleTable.vue";// 定义数据
// 受理基础信息
const applyBasicInfo = ref<ApplyBasicInfo | null>(null)
// 受理样品表格数据
const applySampleTableData = ref<ApplySample[]>([]);
// 滚动到离顶部表头的距离
const applySampleTableScrollTop = ref(0);......</script><template><!-- 提供数据给子类 --><CommonApplySampleTable:apply-sample-list="applySampleTableData":accept-type="applyBasicInfo.acceptType":scroll-top="applySampleTableScrollTop"operate-command-type="info-add" />
</template>

子组件 CommonApplySampleTable.vue

<script setup lang="ts" name="CommonApplySampleTable">
import type { ApplySample } from "@/interface";
import ApplySampleSZTable from "@/components/ApplySampleSZTable.vue";
import ApplySampleOtherTable from "@/components/ApplySampleOtherTable.vue";// 接收父类提供的数据
const props = withDefaults(defineProps<{// 受理样品列表applySampleList: ApplySample[];// 受理类别acceptType: string;// 操作指令类型:新增删除:info-add;修改:info-modify;查看:info-viewoperateCommandType: "info-add" | "info-modify" | "info-view";// 滚动到离顶部表头的距离scrollTop?: number;}>(),{applySampleList: () => [],acceptType: "",operateCommandType: "info-add",scrollTop: 0}
);
</script><template><!-- 受理样品表格,水质类 --><ApplySampleSZTablev-if="props.acceptType.includes(`GD`)":applySampleData="props.applySampleList":applySampleTableScrollTop="props.scrollTop":operate-command-type="props.operateCommandType" /><!-- 受理样品表格,其他 --><ApplySampleOtherTablev-else:applySampleData="props.applySampleList":applySampleTableScrollTop="props.scrollTop":operate-command-type="props.operateCommandType" />
</template><style scoped lang="scss"></style>

祖先传后代模式

父组件 SampleInput.vue

<script setup lang="ts" name="SampleInput">
import { ref, provide, computed } from "vue";
import type { ApplyBasicInfo, Apply, ApplySample } from "@/interface";
import CommonApplySampleTable from "@/components/common/CommonApplySampleTable.vue";// 定义数据
// 受理基础信息
const applyBasicInfo = ref<ApplyBasicInfo | null>(null)
// 受理样品表格数据
const applySampleTableData = ref<ApplySample[]>([]);
// 滚动到离顶部表头的距离
const applySampleTableScrollTop = ref(0);......// 提供数据给后代
// 受理样品列表
provide("applySampleList", applySampleTableData); // applySampleTableData是个Ref对象,不需要.value,传递ref对象本身(响应式数据)
// 受理类别
provide("acceptType",computed(() => applyBasicInfo.value.acceptType) // applyBasicInfo.value.acceptType 是派生数据(属性数据),通过computed提供响应式数据
);
// 操作指令类型:新增删除:info-add;修改:info-modify;查看:info-view
provide("operateCommandType", ref("info-add"));
// 滚动到离顶部表头的距离
provide("scrollTop", applySampleTableScrollTop); // applySampleTableScrollTop是个Ref对象,不需要.value,传递ref对象本身(响应式数据)
</script><template><CommonApplySampleTable />
</template>

子组件 CommonApplySampleTable.vue

<script setup lang="ts" name="CommonApplySampleTable">
import type { ApplySample } from "@/interface";
import ApplySampleSZTable from "@/components/ApplySampleSZTable.vue";
import ApplySampleOtherTable from "@/components/ApplySampleOtherTable.vue";
import { inject, type Ref, ref } from "vue";// 接收祖先提供的数据
// 受理样品列表
const applySampleList = inject<Ref<ApplySample[]>>("applySampleList", ref([]));
// 受理类别
const acceptType = inject<Ref<string>>("acceptType", ref(""));
// 操作指令类型:新增删除:info-add;修改:info-modify;查看:info-view
const operateCommandType = inject<Ref<"info-add" | "info-modify" | "info-view">>("operateCommandType", ref("info-add"));
// 滚动到离顶部表头的距离
const scrollTop = inject<Ref<number>>("scrollTop", ref(0));
</script><template><!-- 受理样品表格,水质类 --><ApplySampleSZTablev-if="acceptType.includes(`GD`)":applySampleData="applySampleList":applySampleTableScrollTop="scrollTop":operate-command-type="operateCommandType" /><!-- 受理样品表格,其他 --><ApplySampleOtherTablev-else:applySampleData="applySampleList":applySampleTableScrollTop="scrollTop":operate-command-type="operateCommandType" />
</template><style scoped lang="scss"></style>

相关文章:

  • 【滑动窗口】串联所有单词的⼦串(hard)
  • 多态的主要好处与不足
  • 10.QT-显示类控件|LCD Number|ProgressBar|Calendar Widget(C++)
  • [论文阅读]Making Retrieval-Augmented Language Models Robust to Irrelevant Context
  • 论文阅读:2024 arxiv DeepInception: Hypnotize Large Language Model to Be Jailbreaker
  • Pandas高级功能
  • C++入门篇(下)
  • 【支付】支付宝支付
  • go+mysql+cocos实现游戏搭建
  • centos停服 迁移centos7.3系统到新搭建的openEuler
  • HTMLCSS实现网页轮播图
  • max31865典型电路
  • 经典算法 表达式求值
  • DEA-Net:基于细节增强卷积和内容引导注意力的单图像去雾
  • 基础理论学习参考
  • 在 Debian 10.x 安装和配置 Samba
  • 论文笔记(七十八)Do generative video models understand physical principles?
  • 2024新版仿蓝奏云网盘源码,已修复已知BUG,样式风格美化,可正常运营生产
  • LeetCode第158题_用Read4读取N个字符 II
  • C语言之机房机位预约系统
  • 廊坊市长:健全依法决策和决策纠错机制,把群众满意作为工作准绳
  • 五一假期出行预订进入高潮:酒店搜索热度翻倍,“请4休11”拼假带动长线游
  • 天工摘得全球首个人形机器人半马冠军:中国机器人产业正努力跑向人机共生社会
  • 8个月女婴被指受虐后体重仅6斤?潮州警方:未发现虐待,父母有抚养意愿
  • 上海奉贤这所九年一贯制学校将迎首批新生,有何特色?
  • 祥源文旅:2024年营业收入约8.64亿元,今年旅游经济总体预期更为乐观