• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

vue3笔记五(:is 动态组件)

武飞扬头像
·某某·
帮助1

1、注意事项

1、在Vue2 的时候is是通过组件名称切换的,在Vue3 setup是通过组件实例切换的

2、如果直接把组件实例放到reactive中代理,vue会发出警告。告知我们可以通过shallowRef 或者 markRaw 跳过proxy 代理。
因为组件实例进行响应式代理毫无意义,且浪费性能
学新通

2、:is 动态组件使用

子组件通过defineProps接受传参

<template>
  <div class="content">
    <div class="tabs" v-for="item in tabArr" :key="item.name" @click="tabChange(item.comName)">
      {{ item.name }}
    </div>
  </div>
  <component :is="currentCom"></component>
</template>

<script setup lang="ts">
  import { ref, reactive, markRaw } from 'vue'
  import A from './A.vue'
  import B from './B.vue'
  import C from './C.vue'

  type Tab = {
    name: string,
    comName: any
  }

  // 从复杂数据类型中取出自己想要的几个属性
  type Com = Pick<Tab, 'comName'>

  const tabArr = reactive<Tab[]>([
    {
      name: 'A组件',
      comName: markRaw(A)
    },
    {
      name: 'B组件',
      comName: markRaw(B)
    },
    {
      name: 'C组件',
      comName: markRaw(C)
    },
  ])

  const currentCom = ref<Com>(tabArr[0].comName)

  const tabChange = (item: Com) => {
    currentCom.value = item
  }
</script>

<style>
  .content {
    display: flex;
    padding: 20px;
    height: 40px;
    width: 100vw;
    background: #f1f1f1;
  }
  .tabs {
    width: 100px;
    height: 40px;
    background: #ffffff;
    margin-right: 5px;
    border: 1px solid #000;
  }
</style>
学新通

原文链接:https://xiaoman.blog.csdn.net/article/details/122891279

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgghggi
系列文章
更多 icon
同类精品
更多 icon
继续加载