0
点赞
收藏
分享

微信扫一扫

setup语法糖将vue2转成vue3【PUSDN低代码】

倚然君 2024-11-02 阅读 37

当然可以!在 Vue 3 中使用 <script lang="ts" setup> 是一种更简洁且类型安全的写法。以下是你的组件使用这种方式重写的版本:

<template>
  <div>
    <a-row align="middle" :gutter="24" class="ivu-mt">
      <a-col
        :xl="colsize.xl"
        :lg="colsize.lg"
        :md="colsize.md"
        :sm="24"
        :xs="24"
        class="ivu-mb"
        v-for="(item, index) in cardLists"
        :key="index"
      >
        <a-card :bordered="false" class="card_cent">
          <div class="card_box">
            <div
              class="card_box_cir"
              :class="circleClass(index)"
            >
              <div
                class="card_box_cir1"
                :class="circleInnerClass(index)"
              >
                <a-icon :type="item.className" />
              </div>
            </div>
            <div class="card_box_txt">
              <span class="sp1">{{ item.count || 0 }}</span>
              <div class="item-group" style="display: flex; justify-content: center; align-items: center;">
                <span class="sp2">{{ item.name }}</span>
                <a-tooltip v-if="item.tip" style="margin-left: 4px;">
                  <template #title>
                    {{ item.tip }}
                  </template>
                  <a-icon type="question-circle-o" style="font-size: 12px; color: gray;" />
                </a-tooltip>
              </div>
            </div>
          </div>
        </a-card>
      </a-col>
    </a-row>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed, defineProps } from 'vue';

// 定义 props 类型
const props = defineProps<{
  cardLists: Array<{
    count: number | string;
    name: string;
    className: string;
    tip?: string;
  }>;
}>();

const colsize = ref({
  xl: 4,
  lg: 8,
  md: 12,
});

// 计算 colsize
const computedColsize = computed(() => {
  const cardLength = props.cardLists.length;
  switch (cardLength) {
    case 1:
      return { xl: 24, lg: 24, md: 24 };
    case 2:
      return { xl: 12, lg: 12, md: 12 };
    case 3:
      return { xl: 8, lg: 8, md: 8 };
    case 4:
      return { xl: 6, lg: 6, md: 12 };
    case 5:
      return { xl: 8, lg: 8, md: 12 };
    default:
      return { xl: 4, lg: 8, md: 12 };
  }
});

// 响应式更新 colsize
colsize.value = computedColsize.value;

// 方法
const circleClass = (index: number) => {
  const classes = ['one', 'two', 'three', 'four', 'five'];
  return classes[index % 5];
};

const circleInnerClass = (index: number) => {
  const classes = ['one1', 'two1', 'three1', 'four1', 'five1'];
  return classes[index % 5];
};
</script>

<style scoped lang="less">
/* 保留你现有的样式 */
</style>

主要变化:

  1. TypeScript 支持:使用 lang="ts",并通过 defineProps 明确指定 cardLists 的类型。
  2. 响应式变量:使用 ref 创建响应式的 colsize
  3. 计算属性:用 computed 创建计算属性,根据 cardLists 的长度计算 colsize
  4. 简化代码<script setup> 语法使代码更加简洁明了。

这种写法结合了 Vue 3 的强大功能和 TypeScript 的类型安全性,非常适合大型项目。如果你有其他问题或者需要进一步的帮助,请随时告诉我!

其他组件引用

要在其他组件中引用这个 Vue 3 组件,你需要确保几个步骤完成:

  1. 导出组件:确保你的组件文件(如 Cards.vue)正常导出。
  2. 导入组件:在需要使用该组件的父组件中导入它。
  3. 注册组件:在父组件中注册该组件。
  4. 使用组件:在父组件的模板中使用该组件。

以下是一个示例,演示如何在父组件中引用 Cards.vue

1. Cards.vue

确保你的 Cards.vue 文件正常,如前所述。

2. ParentComponent.vue

在你的父组件中引用和使用 Cards 组件:

<template>
  <div>
    <h1>我的父组件</h1>
    <Cards :cardLists="cardData" />
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import Cards from './Cards.vue'; // 导入 Cards 组件

// 示例数据
const cardData = ref([
  { count: 10, name: '卡片 1', className: 'icon-class-1', tip: '提示 1' },
  { count: 20, name: '卡片 2', className: 'icon-class-2', tip: '提示 2' },
  { count: 30, name: '卡片 3', className: 'icon-class-3', tip: '提示 3' },
]);
</script>

<style scoped>
/* 你的样式 */
</style>

关键步骤说明:

  1. 导入:使用 import Cards from './Cards.vue'; 导入组件,路径要根据实际位置调整。
  2. 数据准备:在父组件的 setup 中,定义 cardData,并确保它的结构与 Cards 组件的 props 兼容。
  3. 传递 Props:在模板中使用 <Cards :cardLists="cardData" /> 传递数据。

这样就完成了在父组件中引用和使用 Cards 组件的步骤!如果有其他问题或者需要更详细的示例,请告诉我!


举报

相关推荐

0 条评论