vue create xxxx 创建项目(文档地址:https://cli.vuejs.org/zh/guide/creating-a-project.html#vue-create)
按正常开发写一个组件 。。。
使用install函数把组件做成插件方便调用
例:
文件目录
index.js 代码 (自己针对自己的组件做灵活改动)
import UnifiedValidator from './index.vue'
/* istanbul ignore next */
UnifiedValidator.install = function(Vue) {
Vue.component(UnifiedValidator.name, UnifiedValidator);
};
export default UnifiedValidator;
package.json 文件配置如下
打包命令查看 文档 https://cli.vuejs.org/zh/guide/build-targets.html#%E5%BA%94%E7%94%A8
到此组件写完了, 运行yarn component 就打包完成组件啦^ ^
创建npm账号 https://www.npmjs.com 。。。
npm addUser
Username: 用户名
Password: 密码
Email: 邮箱
以上操作完成后 (邮箱需要校验哦)
上传到npm 回到项目根目录 运行npm publish 就可以了
以下为爬坑过程:
code E402
402 Payment Required - PUT http://registry.npmjs.org/@dinghx%2funifiedValidator - You must sign up for private packages
这是因为包名是 @xxx/xxxx npm会认为要发布私包,私包需要收费,需将发布命令改成: npm publish --access public
code E403 没权限发布
npm ERR! [no_perms] Private mode enable, only admin can publish this module [no_perms] Private mode enable, only admin can publish this module: your-package
这个是你的源设置成第三方源的时候才有可能发生,比如设置了淘宝源就可能会导致该问题。只要把源改回默认的就可以了,如下: npm config set registry http://registry.npmjs.org
you must verify your email before publishing a new package: https://www.npmjs.com/email-edit
这是组件需要重新打包
npm ERR! Package name too similar to existing packages; try renaming your package to '@dinghx/captcha' and publishing with 'npm publish --access=public' instead : your-package
这是包名过于相似,需要修改包名
npm ERR! you must verify your email before publishing a new package: https://www.npmjs.com/email-edit : your-package
这是邮箱没有校验,登录https://www.npmjs.com 重新发起邮箱验证