在vue3+setup+ts中使用内置动态组件component

1. vue的内置组件的定义

component是vue的内置组件,不需要注册可以直接使用。
通过is去选择要渲染的组件,可以是一个组件名称字符串(使用在选项式API中),也可以是组件的定义(使用在<script setup>组合式 API中)。

interface DynamicComponentProps {
  is: string | Component
}

2. 使用方法

2-1 基本使用

<template>
  <component :is="number > 0.5 ? Child1 : Child2"></component>
</template>
<script setup lang="ts">
import Child1 from './Child1.vue';
import Child2 from './Child2.vue';
const number = ref(0);
onMounted(() => {
  number.value = Math.random();
});
</script>

2-2 动态组件的传值

在这里插入图片描述

在这里插入图片描述

<template>
  <div class="auto-wrap">
    <div class="tabs-wrap">
      <div
        v-for="item in state.tabs"
        :key="item"
        :class="{ 'active-tabs': state.currentTab == item }"
        class="tabs"
        @click="tabsClick(item)">
        {{ item }}
      </div>
    </div>
    <component :is="showTab" :msg="showMsg" class="content"></component>
  </div>
</template>
<script setup lang="ts">
import Child1 from './Child1.vue';
import Child2 from './Child2.vue';
const state = reactive({
  tabs: ['tab1', 'tab2'],
  currentTab: 'tab1',
});
const tabsClick = (tab: string) => {
  state.currentTab = tab;
};
const showTab = computed(() => {
  return state.currentTab == 'tab1' ? Child1 : Child2; //按定义渲染组件
});
const showMsg = computed(() => {
  return state.currentTab == 'tab1' ? 'tab1' : 'tab2';
});
</script>
<style lang="scss" scoped>
.auto-wrap {
  background: #fff;
  display: flex;
  .tabs-wrap {
    width: 100px;
    height: 50px;
    line-height: 50px;
    .tabs {
      border: 1px solid #eee;
      padding: 3px;
      text-align: center;
    }
    .active-tabs {
      color: red;
    }
  }
  .content {
    padding: 12px;
  }
}
</style>

<!-- Child1.vue -->
<template>
  <div>{{props.msg}}</div>
</template>
<script setup lang="ts">
const props = defineProps<{
  msg?: string;
}>();
</script>
<style scoped></style>

<!-- Child2.vue -->
<template>
  <div>{{ props.msg }}</div>
</template>
<script setup lang="ts">
const props = defineProps<{
  msg?: string;
}>();
</script>
<style scoped></style>

2-3 动态组件的缓存

在组件内添加两个生命周期函数:
在这里插入图片描述

当动态组件的切换过程中,原先保存的状态也会丢失。如果想在组件切换时,保留状态,可以用到<KeepAlive> 内置组件。
作用:防止重复渲染DOM,减少加载时的时间及性能消耗。

使用
    <keep-alive>
      <component :is="showTab" :msg="showMsg" class="content"></component>
    </keep-alive>

<KeepAlive>中可以传入三个参数:
include: 匹配的组件才会被缓存,匹配的模式可以是字符串或者正则表达式
exclude: 匹配的组件都不会被缓存
max:最多可以缓存多少组件实例

interface KeepAliveProps {
  /**
   * 如果指定,则只有与 `include` 名称
   * 匹配的组件才会被缓存。
   */
  include?: MatchPattern
  /**
   * 任何名称与 `exclude`
   * 匹配的组件都不会被缓存。
   */
  exclude?: MatchPattern
  /**
   * 最多可以缓存多少组件实例。
   */
  max?: number | string
}

type MatchPattern = string | RegExp | (string | RegExp)[]

<KeepAlive> 是一个抽象组件,不会被实际渲染成DOM,类似的有<Transition><Teleport>等。

生命周期函数

<KeepAlive> 包裹的组件会多出两个生命周期函数:
onActivated: 组件被激活时调用
onDeactivated: 组件离开时调用
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

activated 在组件挂载时也会调用,并且 deactivated 在组件卸载时也会调用。
这两个钩子不仅适用于 缓存的根组件,也适用于缓存树中的后代组件。

也可以与 <Transition> 一起使用

    <KeepAlive>
      <Transition>
        <component :is="showTab" :msg="showMsg" class="content"></component>
      </Transition>
    </KeepAlive>
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3 + Vite + TypeScript使用EditableProTable组件可以按照以下步骤进行: 1. 首先,确保你已经安装了Ant Design Vue和EditableProTable组件。你可以使用以下命令进行安装: ```bash npm install ant-design-vue@next npm install @ant-design/pro-table@next ``` 2. 在你的Vue组件引入EditableProTable组件和相应的样式: ```vue <template> <a-table-pro :columns="columns" :request="fetchData" :rowKey="record => record.id" :pagination="pagination" :loading="loading" :options="options" :actionRef="actionRef"></a-table-pro> </template> <script> import { defineComponent, ref } from 'vue' import { EditableProTable } from '@ant-design/pro-table' export default defineComponent({ components: { ATablePro: EditableProTable, }, setup() { const columns = [ // 列配置 ] const fetchData = async (params) => { // 发起请求获取数据 } const loading = ref(false) const pagination = ref({ current: 1, pageSize: 10, }) const options = ref({}) const actionRef = ref(null) return { columns, fetchData, loading, pagination, options, actionRef, } }, }) </script> <style lang="less"> @import '~@ant-design/pro-table/dist/component.less'; </style> ``` 3. 根据你的需要,配置EditableProTable的列配置和数据请求逻辑。你可以根据Ant Design Vue的Table组件和EditableProTable的文档进行配置。 这样,在Vue 3 + Vite + TypeScript,你就可以使用EditableProTable组件了。根据你的实际情况,你可能需要进行一些额外的配置和样式调整。请参考Ant Design Vue和EditableProTable的文档进行进一步的学习和调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值