仿 ElementUI 搭建自己的 vue 组件库

仿 ElementUI 搭建自己的 vue 组件库

  • [一、创建 my-ui-can 项目](#一、创建 my-ui-can 项目)
    • [1. 新建项目](#1. 新建项目)
    • [2. 自定义组件](#2. 自定义组件)
    • [3. 创建 MyButton 组件](#3. 创建 MyButton 组件)
    • [4. 导出组件](#4. 导出组件)
    • [5. package.json](#5. package.json)
  • [二、发布到 npm 仓库](#二、发布到 npm 仓库)
    • [1. npm 账号注册(忽略)](#1. npm 账号注册(忽略))
    • [2. 发布 my-ui-can](#2. 发布 my-ui-can)
  • [二、项目引用 my-ui-can 依赖包](#二、项目引用 my-ui-can 依赖包)

功能描述:仿 ElementUI 组件库的方式创建 一个 my-ui-can 的组件库(简单版,支持按需加载,只有 MyButton 组件)。

一、创建 my-ui-can 项目

1. 新建项目

shell 复制代码
vue create my-ui-can

2. 自定义组件

项目目录如下:

my-ui-can/

├── dist/

├── lib/

│ ├── button/

│ │ ├── src/

│ │ │ └── button.vue

│ │ └── index.js

│ └── index.js

│── package.json

└── vue.config.js

3. 创建 MyButton 组件

vue 复制代码
<!-- button.vue -->
<template>
  <button
    class="my-button"
    @click="handleClick"
    :disabled="disabled"
    :autofocus="autofocus"
    :type="nativeType"
  >
    <span v-if="$slots.default"><slot></slot></span>
  </button>
</template>
<script>
  export default {
    name: 'MyButton',
    inject: {
    },
    props: {
      size: String,
      nativeType: {
        type: String,
        default: 'button'
      },
      loading: Boolean,
      disabled: Boolean,
      autofocus: Boolean
    },
    methods: {
      handleClick(evt) {
        this.$emit('click', evt);
      }
    }
  };
</script>
js 复制代码
// lib/button/index.js
import MyButton from './src/button';

/* istanbul ignore next */
MyButton.install = function(Vue) {
  Vue.component(MyButton.name, MyButton);
};

export default MyButton;

4. 导出组件

js 复制代码
// my-ui-can/lib/index.js
import Button from './button/index.js';

const components = [
  Button
];

const install = function(Vue, opts = {}) {
  components.forEach(component => {
    Vue.component(component.name, component);
  });
};

if (typeof window !== 'undefined' && window.Vue) {
  install(window.Vue);
}

export default {
  version: '0.1.0',
  install,
  Button
};

5. package.json

json 复制代码
{
  ....
  "name": "my-ui-can",
  "version": "0.1.0",
  "main": "lib/index.js",
  ...
}

二、发布到 npm 仓库

1. npm 账号注册(忽略)

不知道怎么操作的,可以参考 npm 账户注册

2. 发布 my-ui-can

shell 复制代码
npm config set registry https://registry.npmjs.org/
npm login
npm pulish

二、项目引用 my-ui-can 依赖包

npm install my-ui-can

方式一:全局引入

js 复制代码
// main.js
import MyUI from 'my-ui-can'
Vue.use(MyUI)

方式二:局部引入

vue 复制代码
<template>
  <MyButton>222</MyButton>
</template>
<script>
import MyButton from 'my-ui-can/lib/button'
export default {
  name: 'App',
  components: {
    MyButton
  }
}
</script>
相关推荐
霍夫曼3 分钟前
UTC时间与本地时间转换问题
java·linux·服务器·前端·javascript
VX:Fegn089510 分钟前
计算机毕业设计|基于Java人力资源管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·后端·课程设计
DARLING Zero two♡15 分钟前
浏览器里跑 AI 语音转写?Whisper Web + cpolar让本地服务跑遍全网
前端·人工智能·whisper
Lovely Ruby40 分钟前
前端er Go-Frame 的学习笔记:实现 to-do 功能(三),用 docker 封装成镜像,并且同时启动前后端数据库服务
前端·学习·golang
老华带你飞40 分钟前
健身房|基于springboot + vue健身房管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
JIngJaneIL1 小时前
基于Java酒店预约系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot
深红1 小时前
玩转小程序AR-实战篇
前端·微信小程序·webvr
银空飞羽1 小时前
让Trae SOLO全自主学习开发近期爆出的React RCE漏洞靶场并自主利用验证(CVE-2025-55182)
前端·人工智能·安全
钮钴禄·爱因斯晨1 小时前
DevUI 组件生态与 MateChat 智能应用:企业级前端智能化实战
前端
不会写DN1 小时前
存储管理在开发中有哪些应用?
前端·后端