vue3+ts 动态组件的使用

<script setup lang="ts">
import { reactive, ref, markRaw, shallowRef } from "vue";
import AVue from "./components/A.vue";
import BVue from "./components/B.vue";
import CVue from "./components/C.vue";
const comId = shallowRef(AVue);
const active = ref(0);
const data = reactive([
  {
    name: "A组件",
    com: markRaw(AVue),
  },
  {
    name: "B组件",
    com: markRaw(BVue),
  },
  {
    name: "C组件",
    com: markRaw(CVue),
  },
]);

const switchCom = (item: any, index: number) => {
  comId.value = item.com;
  active.value = index;
};
</script>

<template>
  <div>
    <div style="display: flex">
      <div
        @click="switchCom(item, index)"
        :class="[active === index ? 'active' : '']"
        class="tabs"
        v-for="(item, index) in data"
      >
        <div>{{ item.name }}</div>
      </div>
    </div>
    <div>
      <component :is="comId" :title="'322'" :list="[1]"></component>
    </div>
  </div>
</template>

<style scoped>
.tabs {
  border: 1px solid #ccc;
  padding: 5px 10px;
  margin: 5px;
  cursor: pointer;
}
.active {
  background-color: skyblue;
}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue3是一种流行的JavaScript框架,而TypeScript是一种类型安全的JavaScript超集。结合使用Vue3和TypeScript可以提供更好的开发体验和代码可维护性。 在Vue3中,可以使用TypeScript来编写组件。下面是一个简单的Vue3+TypeScript评论组件的示例: ```typescript <template> <div> <h2>评论列表</h2> <ul> <li v-for="comment in comments" :key="comment.id"> {{ comment.text }} </li> </ul> <form @submit.prevent="addComment"> <input type="text" v-model="newComment" placeholder="请输入评论" /> <button type="submit">添加评论</button> </form> </div> </template> <script lang="ts"> import { defineComponent, ref } from 'vue'; interface Comment { id: number; text: string; } export default defineComponent({ name: 'CommentComponent', data() { return { comments: [] as Comment[], newComment: '', }; }, methods: { addComment() { const newId = this.comments.length + 1; this.comments.push({ id: newId, text: this.newComment }); this.newComment = ''; }, }, }); </script> ``` 在上面的示例中,我们定义了一个名为`CommentComponent`的Vue组件组件包含一个评论列表和一个表单用于添加新的评论。`comments`数组用于存储评论数据,`newComment`变量用于绑定输入框的值。 通过使用`defineComponent`函数和`ref`函数,我们可以定义组件的数据和方法,并将其导出供其他组件使用。 相关问题: 1. Vue3是什么? 2. TypeScriptVue3中的作用是什么? 3. 请解释一下Vue3+TypeScript评论组件的代码逻辑。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你的美,让我痴迷

你的好,我会永远记住你的。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值