image.png

    image.png
    image.png
    image.png
    如果前者是undefiend 就返回[]

    image.png
    Vue3必须是:is setup影响的
    Vue2可以可以is
    image.png
    注册了A组件之后就可以用is=”A”了

    1. <template>
    2. <div class="content">
    3. <div class="tab">
    4. <div
    5. @click="switchItem"
    6. v-for="item in data"
    7. :key="item.name"
    8. class="tab-item"
    9. >
    10. {{ item.name }}
    11. </div>
    12. <component :is="current"></component>
    13. </div>
    14. </div>
    15. </template>
    16. <script setup lang="ts">
    17. import { reactive, ref, markRaw, toRaw } from "vue";
    18. import A from "./A.vue";
    19. import B from "./B.vue";
    20. import C from "./C.vue";
    21. type Tabs = {
    22. name: string;
    23. comName: any;
    24. };
    25. type Com = Pick<Tabs, "comName">;
    26. const data = reactive<Tabs[]>([
    27. { name: "我是A组件", comName: markRaw(A) },
    28. { name: "我是B组件", comName: markRaw(B) },
    29. { name: "我是C组件", comName: markRaw(C) },
    30. ]);
    31. const current = reactive<Com>({
    32. comName: data[0].comName,
    33. });
    34. const switchItem = (item: Tabs) => {
    35. current.comName = item.comName;
    36. };
    37. </script>
    38. <style lang="less" scope>
    39. .tab {
    40. display: flex;
    41. &-item {
    42. border: 1px solid #ccc;
    43. margin-right: 20px;
    44. }
    45. .active {
    46. background: pink;
    47. color: #fff;
    48. }
    49. }
    50. .content {
    51. padding: 20px;
    52. flex: 1;
    53. margin: 20px;
    54. overflow: auto;
    55. border: 1px solid #ccc;
    56. // &-items {
    57. // padding: 20px;
    58. // border: 1px solid #ccc;
    59. // }
    60. }
    61. </style>