本篇文章将介绍如何在组件库中开发一个组件,其中包括

  • 如何本地实时调试组件
  • 如何让组件库支持全局引入
  • 如何在 setup 语法糖下给组件命名
  • 如何开发一个组件

目录结构

packages目录下新建componentsutils两个包,其中components就是我们组件存放的位置,而utils包则是存放一些公共的方法之类的。分别在两个文件下执行pnpm init,并将它们的包名改为@easyest/components@easyest/utils

{
"name": "@easyest/components",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

components目录下新建src目录用于存放所有组件,最终目录结构为

当然这只是目前的结构,后面会进行调整,因为还有样式,测试等文件目录

测试组件

button.vue文件中写一个简单的按钮

<template>
<button>测试按钮</button>
</template>

然后在button/index.ts将其导出

import Button from "./button.vue";
export { Button };
export default Button;

因为我们后面会有很多组件的,比如 Icon,Upload,Select 等,所以我们需要在components/src/index.ts集中导出所有组件

export * from "./button";

最后在components/index.ts导出所有组件提供给外部使用

export * from "./src/index";

接下来我们在上篇文章中搭建的 play 项目中进行一个测试,首先在 paly 项目中本地安装@easyest/components(组件库包名,后续发布可以自己修改名字)

pnpm add @easyest/components

然后再app.vue中引用Button

<template>
<div>
<Button />
</div>
</template>
<script lang="ts" setup>
import { Button } from "@easyest/components";
</script>

启动项目便可以看到 Button 组件了,并且修改 Button 组件也会有热更新的效果

app.use 全局挂载组件

有的时候我们使用组件的时候想要直直接使用 app.use()挂载整个组件库,其实使用 app.use()的时候它会调用传入参数的 install 方法,因此首先我们给每个组件添加一个 install 方法,然后再导出整个组件库,我们将 button/index.ts 改为

import _Button from "./button.vue";
import type { App, Plugin } from "vue";
type SFCWithInstall<T> = T & Plugin;
const withInstall = <T>(comp: T) => {
(comp as SFCWithInstall<T>).install = (app: App) => {
const name = (comp as any).name;
//注册组件
app.component(name, comp as SFCWithInstall<T>);
};
return comp as SFCWithInstall<T>;
};
export const Button = withInstall(_Button);
export default Button;

components/index.ts 修改为

import * as components from "./src/index";
export * from "./src/index";
import { App } from "vue"; export default {
install: (app: App) => {
for (let c in components) {
app.use(components[c]);
}
},
};

此时我们需要给button.vue一个name:ea-button好在全局挂载的时候作为组件名使用

<template>
<button>测试按钮</button>
</template>
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
name: "ea-button",
setup() {
return {};
},
});
</script>

这时候在play/main.ts中全局挂载组件库

import { createApp } from "vue";
import App from "./app.vue";
import easyest from "@easyest/components";
const app = createApp(App);
app.use(easyest);
app.mount("#app");

app.vue 中使用ea-button组件,然后就会发现组件库挂载成功了

<template>
<div>
<ea-button />
</div>
</template>
<script lang="ts" setup></script>

但是这个全局组件并没有任何属性提示,所以我们要借助vscode中的volar给全局组件加上提示效果

首先安装@vue/runtime-core

pnpm add @vue/runtime-core -D -w

在src下新建components.d.ts

import * as components from "./index";
declare module "@vue/runtime-core" {
export interface GlobalComponents {
EaButton: typeof components.Button;
EaIcon: typeof components.Icon;
}
}
export {};

此时全局引入的组件也有了提示效果

注意:当用户使用组件库的时候需要让用户在tsconfig.json中配置types:["easyest/lib/src/components"]才会出现提示效果

"compilerOptions": {
//...
"types": ["easyest/lib/src/components"]
},

使用 setup 语法

我们都知道,使用 setup 语法进行 Vue 组件的开发是非常方便的,但是会有一个问题,就是当我们使用 setup 语法时该怎么给组件命名呢?

其实有两种解决方法,一个是再写一个script标签命名,比如input.vue

<template>
<button>测试按钮</button>
</template>
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
name: "ea-button"
});
</script>
<script lang="ts" setup></script>

这种方式显然是比较奇怪的

第二种方式就是使用插件unplugin-vue-define-options解决,在测试环境中,我们需要把它配置在 play 项目中

首先全局安装unplugin-vue-define-options,因为这个插件后面打包配置也需要用到,最新版本安装会提示错误,看后续作者如何解决吧,暂时用// @ts-ignore忽略

pnpm add unplugin-vue-define-options  -D -w

然后在play/vite.config.ts引入该插件

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
// @ts-ignore
import DefineOptions from "unplugin-vue-define-options/vite";
export default defineConfig({
plugins: [vue(), DefineOptions()],
});

此时我们便可以直接使用defineOptions函数定义组件名了

<template>
<button>测试按钮</button>
</template> <script lang="ts" setup>
defineOptions({ name: "ea-button" });
</script>

组件开发

我们都知道一个组件需要接受一些参数来实现不同效果,比如 Button 组件就需要接收typesizeround等属性,这里我们暂且只接收一个属性type来开发一个简单的 Button 组件。

我们可以根据传入的不同type来赋予 Button 组件不同类名

// button.vue
<template>
<button class="ea-button" :class="buttonStyle"><slot /></button>
</template> <script lang="ts" setup>
import "./style/index.less";
import { computed } from "vue";
defineOptions({ name: "ea-button" });
type ButtonProps = {
type?: string;
};
const buttonProps = defineProps<ButtonProps>(); const buttonStyle = computed(() => {
return { [`ea-button--${buttonProps.type}`]: buttonProps.type };
});
</script>

这里引入了样式文件,在 button 目录下新建 style 文件夹来存放 Button 组件的样式

src/button/style/index.less如下

.ea-button {
display: inline-block;
line-height: 1;
white-space: nowrap;
cursor: pointer;
background: #fff;
border: 1px solid #dcdfe6;
color: #606266;
-webkit-appearance: none;
text-align: center;
box-sizing: border-box;
outline: none;
margin: 0;
transition: 0.1s;
font-weight: 500;
padding: 12px 20px;
font-size: 14px;
border-radius: 4px;
} .ea-button.ea-button--primary {
color: #fff;
background-color: #409eff;
border-color: #409eff; &:hover {
background: #66b1ff;
border-color: #66b1ff;
color: #fff;
}
}

此时在 app.vue 中引入 Button 组件就可以看到想要的效果了

<template>
<div>
<Button type="primary">主要按钮</Button>
</div>
</template>
<script lang="ts" setup>
import { Button } from "@easyest/components";
</script>

由于组件的开发可能涉及的内容比较多,这里就不详细展开,这里只简单介绍一下组件开发的大致思路,后续会专门对一些常用组件进行开发,欢迎点赞收藏加关注!

本文对应代码地址 如何开发一个组件,动动小手Star一下谢谢

关注公众号web前端进阶 查看完整教程

从0搭建Vue3组件库(四): 如何开发一个组件的更多相关文章

  1. vue组件库的基本开发步骤

    市面上目前已有各种各样的UI组件库,比如 Element 和 iView,他们的强大毋庸置疑.但是我们面临的情况是需求越来越复杂,当它们不能再满足我们需求的时候,这个时候就有必要开发一套属于自己团队的 ...

  2. 从0搭建Vue3组件库:button组件

    button组件几乎是每个组件库都有的:其实实现一个button组件是很简单的.本篇文章将带你一步一步的实现一个button组件.如果你想了解完整的组件库搭建,你可以先看使用Vite和TypeScri ...

  3. 从0搭建vue3组件库:Shake抖动组件

    先看下效果 其实就是个抖动效果组件,实现起来也非常简单.之所以做这样一个组件是为了后面写Form表单的时候会用到它做一个规则校验,比如下面一个简单的登录页面,当点击登录会提示用户哪个信息没输入,当然这 ...

  4. 从0搭建vue3组件库: 如何完整搭建一个前端脚手架?

    相信大家在前端开发中都使用过很多前端脚手架,如vue-cli,create-vite,create-vue等:本篇文章将会为大家详细介绍这些前端脚手架是如何实现的,并且从零实现一个create-kit ...

  5. 从0搭建vue3组件库:自动化发布、管理版本号、生成 changelog、tag

    今天看到一篇文章中提到了一个好用的工具release-it.刚好可以用在我正在开发的vue3组件库.纸上得来终觉浅,绝知此事要躬行,说干就干,下面就介绍如何将release-it应用到实际项目中,让组 ...

  6. 从0搭建vue3组件库: Input组件

    本篇文章将为我们的组件库添加一个新成员:Input组件.其中Input组件要实现的功能有: 基础用法 禁用状态 尺寸大小 输入长度 可清空 密码框 带Icon的输入框 文本域 自适应文本高度的文本域 ...

  7. wussUI v1.0.0小程序UI组件库 第一期开发已完成

    经过了两个月不到的开发时间,我们phonycode团队顺利的发布了小程序的UI组件库 wuss-ui 的第一个版本.目前大体预览如下 介绍 wussUI 现在有大概27个组件左右, 目前基础组件都有了 ...

  8. 微信小程序 MinUI 组件库系列之 price 价格组件

    MinUI 是基于微信小程序自定义组件特性开发而成的一套简洁.易用.高效的组件库,适用场景广,覆盖小程序原生框架.小程序组件化框架等,并且提供了高效的命令行工具.MinUI 组件库包含了很多基础的组件 ...

  9. Blazor Bootstrap 组件库 Toast 轻量弹窗组件介绍

    轻量级 Toast 弹窗 DEMO https://www.blazor.zone/toasts 基础用法: 用户操作时,右下角给予适当的提示信息 <ToastBox class="d ...

  10. vue组件库的基本开发步骤(源代码)

    上次发布的随笔忘记提供源代码了,今天特地来补充,如果有什么问题,欢迎大家为我修改指正. vue.config.js文件: const path = require('path') function r ...

随机推荐

  1. MySQL进阶实战7,查询的执行过程

    @ 目录 一.拆分查询 二.分解关联查询 三.查询的执行过程 四.优化器的一些优化手段 1.重新定义关联表的顺序 2.将外连接转化为内连接 3.使用增加变换规则 4.优化count().max().m ...

  2. 基于.NetCore开发博客项目 StarBlog - (22) 开发博客文章相关接口

    前言 本文介绍博客文章相关接口的开发,作为接口开发介绍的第一篇,会写得比较详细,以抛砖引玉,后面的其他接口就粗略带过了,着重于WebApi开发的周边设施. 涉及到的接口:文章CRUD.置顶文章.推荐文 ...

  3. 开发一个MyBatis通用Mapper的轮子

    一.前言 程序猿为什么如此执着于造轮子?MyBatis-Plus如此强大的工具流行这么多年了,我为啥还在重复造这样的轮子? 1.公司的技术规范不允许使用MyBatis-Plus,咱也不知道什么原因: ...

  4. 使用腾讯云部署war包

    目录 1.前期准备 2.springboot打war包 3.部署war包 4.导入数据库 5.修改Tomcat启动端口 6.启动服务器 7.设置腾讯云服务器防火墙规则 8.从外部访问 9.总结 10. ...

  5. 基本能看懂的C编译器,只有365行!

    Fabrice Bellard is a French computer programmer known for writing FFmpeg, QEMU, and the Tiny C Compi ...

  6. 基于Linux下的Ubuntu操作系统常用命令

    一 .linux操作系统的特点 1.linux下一切皆文件 2.linux系统就像一个倒置数 3.linux系统支持多用户.多任务 二. Ubuntu --"乌班图"操作系统 Ub ...

  7. 初学《python编程从入门到实践》web应用程序,出现错误

    一开始是遇到了TemplateDoesNotExist的错误,上百度都是说改settings.py里面的TEMPLATE的DIRS, 但我改了还是出现问题, 我用的<python编程从入门到实践 ...

  8. JS生成下载文件

    常用方式: /** * 生成下载文件 * @param {Object} filename * @param {Object} text */ function download(filename, ...

  9. SQL中常用函数操作

    --在SQL SERVER中批量替换字符串的方法 update [Table] set [Field] = REPLACE([Field],'被替换的原内容','要替换的内容') update HBb ...

  10. java入门与进阶P-5.5+P-5.6

    投票统计 写一个程序,输入数量不确定的[0,9]范围内的整数,统计每一中数字出现的次数,输入-1表示结束 循环遍历 通常都是使用for循环,让循环变量i从0到<数组的length,这样循环体内最 ...