1.创建项目
vue create mynpm
2.写组件
3.在src目录下创建index.js,代码如下,根据实际情况修改
import Vue from "vue"
import mycomponent from './components/mycomponent .vue'
let MyPlugin = {
version: '1.0.0'
};
MyPlugin.install = function(Vue) {
if (this.installed) return;
Vue.component(mycomponent.name, mycomponent)
};
// auto install
if (typeof window !== 'undefined' && window.Vue) {
MyPlugin.install(window.Vue);
}
Vue.use(MyPlugin)
export default MyPlugin
4.修改package.json
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"build-npm": "vue-cli-service build --target lib --name mynpm ./src/index.js"
},
"main": "dist/mynpm.common.js",
"private": false,
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/xxx/xxx"
}
5.运行 build-npm 命令打包
6.登录npm
npm login
没账号的话先去注册 npm官网地址
7.发布
npm publish
8.安装
发布成功后就可以通过npm安装了
npm install mycomponent -S
9.使用
import 'mycomponent'