温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

基于Vue3怎么实现印章徽章组件

发布时间:2023-04-28 10:42:46 阅读:166 作者:iii 栏目:开发技术
Vue开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

本文小编为大家详细介绍“基于Vue3怎么实现印章徽章组件”,内容详细,步骤清晰,细节处理妥当,希望这篇“基于Vue3怎么实现印章徽章组件”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

需要实现的组件效果:

基于Vue3怎么实现印章徽章组件

该组件有设置颜色、大小、旋转度数和文本内容功能。

一、组件实现代码

组件代码文件结构

基于Vue3怎么实现印章徽章组件

src/components/StampBadge/src/StampBadge.vue 文件代码

<template>
  <div
    class="first-ring"
    v-bind="getBindValue"
    :class="getStampBadgeClass"
    :
  >
    <div class="second-ring" :class="getStampBadgeClass">
      <div class="third-ring" :class="getStampBadgeClass">
        <div class="forth-ring" :class="getStampBadgeClass">
          <div class="content-rectangle ellipsis" :class="getStampBadgeClass">
            <span class="">{{ content }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
  import { defineComponent } from "vue";
  export default defineComponent({
    name"StampBadge",
    inheritAttrsfalse,
  });
</script>

<script lang="ts" setup>
  import { computed, unref } from "vue";
  import { stampBadgeProps } from "./props";
  import { useAttrs } from "/@/hooks/core/useAttrs";

  const props = defineProps(stampBadgeProps);
  // get component class
  const attrs = useAttrs({ excludeDefaultKeysfalse });
  const getStampBadgeClass = computed(() => {
    const { color, size } = props;
    return [
      {
        [`stamp-badge-${color}`]: !!color,
        [`stamp-badge-${size}`]: !!size,
      },
    ];
  });

  // get inherit binding value
  const getBindValue = computed(() => ({ ...unref(attrs), ...props }));
</script>

<style lang="less" scoped>
  .first-ring {
    border-radius100px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .second-ring {
    background#fff;
    border-radius100px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .third-ring {
    border-radius100px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .forth-ring {
    background#fff;
    border-radius100px;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
  }

  .content-rectangle {
    background#fff;
    font-weight: bold;
    text-align: center;
    position: absolute;
  }

  .ellipsis {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  // primary
  .stamp-badge-primary.first-ring {
    background#1890ff;
  }
  .stamp-badge-primary.third-ring {
    background#1890ff;
  }
  .stamp-badge-primary.content-rectangle {
    border1px solid #1890ff;
    color#1890ff;
  }
  // success
  .stamp-badge-success.first-ring {
    background#52c41a;
  }
  .stamp-badge-success.third-ring {
    background#52c41a;
  }
  .stamp-badge-success.content-rectangle {
    border1px solid #52c41a;
    color#52c41a;
  }
  // error
  .stamp-badge-error.first-ring {
    background#ff4d4f;
  }
  .stamp-badge-error.third-ring {
    background#ff4d4f;
  }
  .stamp-badge-error.content-rectangle {
    border1px solid #ff4d4f;
    color#ff4d4f;
  }
  // warning
  .stamp-badge-warning.first-ring {
    background#faad14;
  }
  .stamp-badge-warning.third-ring {
    background#faad14;
  }
  .stamp-badge-warning.content-rectangle {
    border1px solid #faad14;
    color#faad14;
  }
  // info
  .stamp-badge-info.first-ring {
    background#ccc;
  }
  .stamp-badge-info.third-ring {
    background#ccc;
  }
  .stamp-badge-info.content-rectangle {
    border1px solid #ccc;
    color#ccc;
  }
  // large
  .stamp-badge-large.first-ring {
    width84px;
    height84px;
  }
  .stamp-badge-large.second-ring {
    width80px;
    height80px;
  }
  .stamp-badge-large.third-ring {
    width74px;
    height74px;
  }
  .stamp-badge-large.forth-ring {
    width64px;
    height64px;
  }
  .stamp-badge-large.content-rectangle {
    width90px;
    font-size1.2rem;
  }
  // middle
  .stamp-badge-middle.first-ring {
    width64px;
    height64px;
  }
  .stamp-badge-middle.second-ring {
    width60px;
    height60px;
  }
  .stamp-badge-middle.third-ring {
    width56px;
    height56px;
  }
  .stamp-badge-middle.forth-ring {
    width48px;
    height48px;
  }
  .stamp-badge-middle.content-rectangle {
    width70px;
    font-size1rem;
  }
  // small
  .stamp-badge-small.first-ring {
    width54px;
    height54px;
  }
  .stamp-badge-small.second-ring {
    width50px;
    height50px;
  }
  .stamp-badge-small.third-ring {
    width46px;
    height46px;
  }
  .stamp-badge-small.forth-ring {
    width38px;
    height38px;
  }
  .stamp-badge-small.content-rectangle {
    width60px;
    font-size0.8rem;
  }
</style>

src/components/StampBadge/src/props.ts 文件代码

export const stampBadgeProps = {
  color: {
    typeString,
    default"primary",
    validator(v) =>
      ["primary""error""warning""success""info"].includes(v),
  },
  /**
   * stamp badge size.
   * @default: middle
   */
  size: {
    typeString,
    default"middle",
    validator(v) => ["large""middle""small"].includes(v),
  },
  /**
   * stamp badge rotate deg.
   * @default: 0
   */
  rotate: { typeNumberdefault0 },
  content: { typeStringdefault"Unknown" },
};

src/components/StampBadge/index.ts 文件代码

import { withInstall } from "/@/utils";
import type { ExtractPropTypes } from "vue";
import stampbadge from "./src/StampBadge.vue";
import { stampBadgeProps } from "./src/props";

export const StampBadge = withInstall(stampbadge);
export declare type ButtonProps = Partial<
  ExtractPropTypes<typeof stampBadgeProps>
>;

src/utils/index.ts 文件代码

export const withInstall = <T>(component: T, alias?: string) => {
  const comp = component as any;
  comp.install = (app: App) => {
    app.component(comp.name || comp.displayName, component);
    if (alias) {
      app.config.globalProperties[alias] = component;
    }
  };
  return component as T & Plugin;
};

二、组件全局注册代码

src/components/registerGlobComp.ts 文件代码

import type { App } from "vue";
import { StampBadge } from "./StampBadge";

export function registerGlobComp(app: App) {
  app.use(StampBadge);
}

src/main.ts 文件代码

import { createApp } from "vue";
import App from "./App.vue";
import { registerGlobComp } from "/@/components/registerGlobComp";

async function bootstrap() {
  // 创建应用实例
  const app = createApp(App);
  // 注册全局组件
  registerGlobComp(app);
  // 挂载应用
  app.mount("#app"true);
}

bootstrap();

三、组件应用代码

<div >
  <StampBadge
    
    size="middle"
    color="success"
    content="已建档"
    :rotate="45"
  />
</div>

读到这里,这篇“基于Vue3怎么实现印章徽章组件”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:https://www.cnblogs.com/yuzhihui/p/17359323.html

AI

开发者交流群×