如何从0撸出一个Vue组件库并发布到npm
如何从0撸出一个Vue组件库并发布到npm?下面本篇文章手把手带大家从0开发一个Vue组件库,看看怎么发布到npm,希望对大家有所帮助。
1、新建文件夹在终端打开执行 npm init -y
生成package.json如下,注意如果要发布到npm,name不能有下划线,大写字母等
{ "name": "vuecomponentdi", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }
2、建立目录结构
目录结构如下
-- vueComponentDi -- packages -- button -- index.js -- index.vue -- toast -- index.js -- index.vue -- index.js -- package.json
3、本地调试
- vueComponentDi/index.js
export default function(){ console.log('本地调试') }
- 新建一个vue项目
vue create testvue
在testvue下的package.json下的测试依赖devDependencies添加vueComponentDi/index.js绝对地址
"devDependencies": { ... "vuecomponentdi": "F:/vueComponent@Di/vueComponentDi",//根据自己实际项目地址填写 ... }
- 执行npm link
在testvue执行npm link将vuecomponentdi软链接到node_modules中
- vuecomponentdi安装Eslint
由于testvue引入组件会进行Eslint检测,不安装会报错(testvue关闭Eslint可省略这一步)
安装方法:
npm install eslint@6.7.2 --save-dev ./node_modules/.bin/eslint --init
- 在testvue使用vuecomponentdi
import test from "vuecomponentdi"
<template> <div class="home"> <img alt="Vue logo" src="../assets/logo.png"> <HelloWorld msg="Welcome to Your Vue.js App"/> </div> </template> <script> // @ is an alias to /src import HelloWorld from '@/components/HelloWorld.vue' import test from "vuecomponentdi" export default { name: 'Home', components: { HelloWorld }, created(){ test() } } </script>
控制台打印>本地调试
4、开发一个button组件
- button模块:进入vueComponentDi/packages/button/index.vue
type只支持传入primary属性,v-on="listeners" 传入内部组件
<template> <div> <button class="di-button" v-on="$listeners" :class="[type?`di-button--${type}`:'']"><slot></slot></button> </div> </template> <script> export default { name:"di-button", props:{ type:String } } </script> <style> .di-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: .1s; font-weight: 500; -moz-user-select: none; -webkit-user-select: none; -ms-user-select: none; padding: 12px 20px; font-size: 14px; border-radius: 4px; } .di-button:focus, .di-button:hover { color: #409eff; border-color: #c6e2ff; background-color: #ecf5ff; } .di-button:active { color: #3a8ee6; border-color: #3a8ee6; outline: none; } .di-button--primary { color: #fff; background-color: #409eff; border-color: #409eff; } .di-button--primary:focus, .di-button--primary:hover { background: #66b1ff; border-color: #66b1ff; color: #fff; } .di-button--primary.is-active, .di-button--primary:active { background: #3a8ee6; border-color: #3a8ee6; color: #fff; } </style>
- button模块导出:进入vueComponentDi/packages/button/index.js
如果导出一个带有install函数的对象,则在Vue2中可以直接使用Vue.use(xx)调用此函数,既执行 Vue.component(name,option)创建了一个组件
import button from "./index.vue" button.install=(Vue)=>{ Vue.component(button.name,button) } export default button
- 聚合导出button:进入vueComponentDi/index.js
因为开发的组件不止一个,所以需要在入口文件统一导出
import diButton from "./packages/button" export { diButton }
- 在testvue使用
<template> <div class="home"> <di-button type="primary">按钮</di-button> </div> </template> <script> // @ is an alias to /src import Vue from 'vue' import {diButton} from "vuecomponentdi" Vue.use(diButton) export default { name: 'Home' } </script>
5、开发一个toast弹窗
- toast模块:vueComponentDi/packages/toast/index.vue
type只支持warning和success
<template> <div class="di-toast" :class="`di-toast--${type}`" v-if="show"> {{message}} </div> </template> <script> export default { data(){ return { message:'', show:false, type:'' } } } </script> <style> .di-toast{ width: 60%; width: 200px; background: rgb(15, 15, 15); padding:3px; text-align: center; color: #fff; border-radius: 10px; position: fixed; left: calc(50% - 100px); top: 200px; } .di-toast--warning{ background: #FDF6EC; color: #E6A28B; } .di-toast--success{ background: #F0F9EB; color: #93C26D; } </style>
- toast模块导出:vueComponentDi/packages/toast/index.js
因为toast弹窗需要在vue中支持this.$toast调用,所以用了Vue.extend方法,这个 API 在日常开发中很少使用,一般在开发组件的时候它才能派上用场,官方定义:使用基础 Vue 构造器,创建一个“子类”。参数是一个包含组件选项的对象
import toast from './index.vue' toast.install = (Vue) => { const toastConstructor = Vue.extend(toast);//使用基础 Vue 构造器,创建一个“子类”。参数是一个包含组件选项的对象。 let $vm = new toastConstructor();//将这个子类实例化 let $el = $vm.$mount().$el;//$vm执行$mount()手动挂载会返回一个带有$el的对象,$el就是一个dom对象 document.querySelector("body").appendChild($el);//将这个dom对象添加到body中 //在Vue原型上注入$toast方法 Vue.prototype.$toast = (option) => { $vm.show = true if (!(option instanceof Object)) { //如果传的不是对象直接弹出 $vm.message = option } else { $vm.message = option.message $vm.type = option.type } setTimeout(() => { $vm.show = false }, 2000) } } export default toast
- 聚合导出:vueComponentDi/index.js
import diButton from "./packages/button" import toast from "./packages/toast" export { diButton, toast }
- vuetest中使用toast
<template> <div class="home"> <di-button type="primary" @click="toast">按钮</di-button> </div> </template> <script> // @ is an alias to /src import Vue from "vue"; import { diButton, toast } from "vuecomponentdi"; Vue.use(diButton); Vue.use(toast); export default { name: "Home", methods: { toast() { // this.toast("这是一个普通弹窗"); // this.$toast({ // message: "这是一个成功弹窗", // type: "success", // }); this.$toast({ message: "这是一个警告弹窗", type: "warning", }); }, }, }; </script>
6、发布到npm
- 公有配置
组件开发完成需要发布到npm以便于别人使用;因为发布的是公有包,所以需要在vueComponentDi/package.json中配置
"publishConfig": { "access": "public" },
完整package.json:
{ "name": "vuecomponentdi", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "eslint": "^6.7.2", "eslint-plugin-vue": "^8.2.0" }, "publishConfig": { "access": "public" } }
- 发布
npm发布很简单,只需要两个命令:
npm login npm publish
执行npm login需要你有npm账号,可以到 npm官网 注册
npm官网地址:https://www.npmjs.com/
发布完成之后就可以在任何一个vue2项目中安装使用了:
npm install vuecomponentdi
git地址: vue组件开发(https://gitee.com/geeksdidi/vue-component-di)
【相关推荐:vue.js教程】
以上是如何从0撸出一个Vue组件库并发布到npm的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

随着前端技术的不断发展,Vue已经成为了前端开发中的热门框架之一。在Vue中,组件是其中的核心概念之一,它可以将页面分解为更小,更易管理的部分,从而提高开发效率和代码复用性。本文将重点介绍Vue如何实现组件的复用和扩展。一、Vue组件复用mixinsmixins是Vue中的一种共享组件选项的方式。Mixins允许将多个组件的组件选项合并成一个对象,从而最大

Vue组件通信:使用$destroy进行组件销毁通信在Vue开发中,组件通信是非常重要的一个方面。Vue提供了多种方式来实现组件通信,比如props和emit、vuex等。本文将介绍另一种组件通信方式:使用$destroy进行组件销毁通信。在Vue中,每个组件都有一个生命周期,其中包含了一系列的生命周期钩子函数。组件的销毁也是其中之一,Vue提供了一个$de

Vue实战:日期选择器组件开发引言:日期选择器是在日常开发中经常用到的一个组件,它可以方便地选择日期,并提供各种配置选项。本文将介绍如何使用Vue框架来开发一个简单的日期选择器组件,并提供具体的代码示例。一、需求分析在开始开发之前,我们需要进行需求分析,明确组件的功能和特性。根据常见的日期选择器组件功能,我们需要实现以下几个功能点:基础功能:能够选择日期,并

Vue组件通信:使用watch和computed进行数据监听Vue.js是一款流行的JavaScript框架,它的核心思想是组件化。在一个Vue应用中,不同的组件之间需要进行数据的传递和通信。在这篇文章中,我们将介绍如何使用Vue的watch和computed来进行数据的监听和响应。watch在Vue中,watch是一个选项,它可以用来监听一个或多个属性的变

Vue是一款流行的JavaScript框架,它提供了丰富的工具和功能,可以帮助我们构建现代化的Web应用程序。尽管Vue本身已经提供了许多实用的功能,但有时候我们可能需要使用第三方库来扩展Vue的能力。本文将介绍在Vue项目中如何使用第三方库,并提供具体的代码示例。1.引入第三方库在Vue项目中使用第三方库的第一步是引入它们。我们可以通过以下几种方式来引入

深入理解Vue的组件生命周期,需要具体代码示例引言:Vue.js是一款渐进式JavaScript框架,以其简洁易学、高效灵活的特性而备受开发者的青睐。在Vue的组件化开发中,了解组件的生命周期是重要的一环。本文将深入探讨Vue组件的生命周期,并提供具体的代码示例,帮助读者更好地理解和应用。一、Vue组件的生命周期图示Vue组件的生命周期可以看做是组件

Vue组件中如何实现多种数据交互方式的切换在Vue组件开发中,经常会遇到需要切换不同的数据交互方式的场景,比如通过API请求数据、通过表单输入数据或者通过WebSocket实时推送数据等等。本文将介绍如何在Vue组件中实现这种多种数据交互方式的切换,并且会提供具体的代码示例。方式一:API请求数据在某些情况下,我们需要通过API请求数据来获取后台的数据。下面

在Vue开发中,我们经常会遇到处理复杂数据结构和算法的情况。这些问题可能涉及大量的数据操作、数据同步、性能优化等方面。本文将介绍一些处理复杂数据结构和算法的注意事项和技巧,帮助开发者更好地应对这些挑战。一、数据结构的选择在处理复杂数据结构和算法时,选择合适的数据结构非常重要。Vue提供了丰富的数据结构和方法,开发者可以根据实际需求选择合适的数据结构。常用的数
