当前位置: 首页 > news >正文

仿 ElementUI 搭建自己的 vue 组件库

仿 ElementUI 搭建自己的 vue 组件库

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

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

一、创建 my-ui-can 项目

1. 新建项目

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 组件

<!-- button.vue -->
<template><buttonclass="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>
// 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. 导出组件

// 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

{...."name": "my-ui-can","version": "0.1.0","main": "lib/index.js",...
}

二、发布到 npm 仓库

1. npm 账号注册(忽略)

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

2. 发布 my-ui-can

npm config set registry https://registry.npmjs.org/
npm login
npm pulish

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

npm install my-ui-can

方式一:全局引入

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

方式二:局部引入

<template><MyButton>222</MyButton>
</template>
<script>
import MyButton from 'my-ui-can/lib/button'
export default {name: 'App',components: {MyButton}
}
</script>

相关文章:

  • Selenium入门之环境搭建
  • 2023 (ICPC) Jiangxi Provincial Contest ABCHIJKL
  • Linux内核内存管理单元 详解Linux 内核伙伴系统(Buddy System)的快速路径分配函数get_page_from_freelist
  • LeetCode 2537.统计好子数组的数目:滑动窗口(双指针)
  • HackMyVM - TryHarder
  • Linux》》bash 、sh 执行脚本
  • 大厂面试:六大排序
  • 各种排序算法
  • 从 BI 与 SQL2API 的差异,看数据技术的多元发展路径
  • 网络原理 - 初识网络 1
  • vue3.2 + element-plus 实现跟随input输入框的弹框,弹框里可以分组或tab形式显示选项
  • 操作系统之shell实现(上)
  • 力扣DAY52-54 | 热100 | 图论:腐烂的橘子、课程表、前缀树
  • 解决Flutter 2.10.5在升级Xcode 16后的各种报错
  • Linux网络编程第一课:深入浅出TCP/IP协议簇与网络寻址系统
  • 源码分析之Leaflet中Map类扩展方法之ScrollWheelZoom
  • Redis核心数据类型在实际项目中的典型应用场景解析
  • git UserInterfaceState.xcuserstate 文件频繁更新
  • 【正点原子STM32MP257连载】第四章 ATK-DLMP257B功能测试——IMX335摄像头测试 #MIPI CSI
  • lodash-es 详解
  • 微网站开发语言/今天今日新闻头条最新消息
  • 西安曲江文化园区建设开发有限公司网站/农夫山泉软文300字
  • p2p网站建设需求/合肥瑶海区房价
  • 网站建设过程的结构图/北京seo优化wyhseo
  • 电子商务门户网站的研究与实现/查网站关键词工具
  • 微信app下载安装官方版2021/seo黑帽教学网