Home>Article>Web Front-end> How to create a Vue component library from scratch and publish it to npm
How to create a Vue component library from scratch and publish it to npm? The following article will guide you step by step in developing a Vue component library from scratch and see how to publish it to npm. I hope it will be helpful to you.
1. Create a new folder, open it in the terminal and executenpm init -y
Generate package.json as follows. Note that if you want to publish it to npm, the name cannot have underscores, capital letters, etc.
{ "name": "vuecomponentdi", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }
2. Create a directory structure
The directory structure is as follows
-- vueComponentDi -- packages -- button -- index.js -- index.vue -- toast -- index.js -- index.vue -- index.js -- package.json
3. Local debugging
export default function(){ console.log('本地调试') }
vue create testvue
Add vueComponentDi/ to the test dependencies devDependencies under package.json under testvue index.js absolute address
"devDependencies": { ... "vuecomponentdi": "F:/vueComponent@Di/vueComponentDi",//根据自己实际项目地址填写 ... }
Execute npm link in testvue to soft link vuecomponentdi to node_modules
Since testvue introduces components, Eslint detection will be performed, and an error will be reported if it is not installed (testvue can omit this step by closing Eslint)
Installation method:
npm install eslint@6.7.2 --save-dev ./node_modules/.bin/eslint --init
import test from "vuecomponentdi"
Console Print>Local debugging
4. Develop a button component
type只支持传入primary属性,v-on=" listeners" 传入内部组件
如果导出一个带有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
因为开发的组件不止一个,所以需要在入口文件统一导出
import diButton from "./packages/button" export { diButton }
按钮
5、开发一个toast弹窗
type只支持warning和success
{{message}}
因为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
import diButton from "./packages/button" import toast from "./packages/toast" export { diButton, toast }
按钮
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教程】
The above is the detailed content of How to create a Vue component library from scratch and publish it to npm. For more information, please follow other related articles on the PHP Chinese website!