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

父传子模式

父组件 SampleInput.vue

javascript 复制代码
<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

javascript 复制代码
<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-view
    operateCommandType: "info-add" | "info-modify" | "info-view";
    // 滚动到离顶部表头的距离
    scrollTop?: number;
  }>(),
  {
    applySampleList: () => [],
    acceptType: "",
    operateCommandType: "info-add",
    scrollTop: 0
  }
);
</script>

<template>
  <!-- 受理样品表格,水质类 -->
  <ApplySampleSZTable
    v-if="props.acceptType.includes(`GD`)"
    :applySampleData="props.applySampleList"
    :applySampleTableScrollTop="props.scrollTop"
    :operate-command-type="props.operateCommandType" />
  <!-- 受理样品表格,其他 -->
  <ApplySampleOtherTable
    v-else
    :applySampleData="props.applySampleList"
    :applySampleTableScrollTop="props.scrollTop"
    :operate-command-type="props.operateCommandType" />
</template>

<style scoped lang="scss"></style>

祖先传后代模式

父组件 SampleInput.vue

javascript 复制代码
<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

javascript 复制代码
<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>
  <!-- 受理样品表格,水质类 -->
  <ApplySampleSZTable
    v-if="acceptType.includes(`GD`)"
    :applySampleData="applySampleList"
    :applySampleTableScrollTop="scrollTop"
    :operate-command-type="operateCommandType" />
  <!-- 受理样品表格,其他 -->
  <ApplySampleOtherTable
    v-else
    :applySampleData="applySampleList"
    :applySampleTableScrollTop="scrollTop"
    :operate-command-type="operateCommandType" />
</template>

<style scoped lang="scss"></style>
相关推荐
universe_017 分钟前
day25|学习前端js
前端·笔记
Zuckjet12 分钟前
V8 引擎的性能魔法:JSON 序列化的 2 倍速度提升之路
前端·chrome·v8
MrSkye13 分钟前
🔥React 新手必看!useRef 竟然不能触发 onChange?原来是这个原因!
前端·react.js·面试
wayman_he_何大民20 分钟前
初识机器学习算法 - AUM时间序列分析
前端·人工智能
juejin_cn21 分钟前
前端使用模糊搜索fuse.js和拼音搜索pinyin-match提升搜索体验
前端
....4921 小时前
Vue3 + Element Plus 实现可搜索、可折叠、可拖拽的部门树组件
前端·javascript·vue.js
teeeeeeemo1 小时前
如何做HTTP优化
前端·网络·笔记·网络协议·http
范范之交1 小时前
JavaScript基础语法two
开发语言·前端·javascript
界面开发小八哥2 小时前
DevExtreme Angular UI控件更新:引入全新严格类型配置组件
前端·ui·界面控件·angular.js·devexpress
bitbitDown2 小时前
重构缓存时踩的坑:注释了三行没用的代码却导致白屏
前端·javascript·vue.js