利用vue/cli先创建项目
新建component, 并且可以在其他页面中引入,查看组件是否正常
<template><div> <h1>vue-com-test</h1> </div></template><script> export default { name: 'vueComTestAlvin' }</script><style lang="scss" scoped></style>
创建install.js文件
// install.jsimport vueComTestAlvin from './components/vueComTestAlvin'export default { install: (Vue) => { Vue.component('vue-com-test-alvin', vueComTestAlvin) }}
修改package.json
{ "name": "vue-com-test-alvin", "version": "0.1.3", "main": "dist/vue-com-test-alvin.common.js", // 组件引入的入口 "files": ["dist/"], // 发布到npm时的文件 "scripts": { // 打包 "build-library": "vue-cli-service build --target lib --name vue-com-test-alvin ./src/install.js", "dev": "vue-cli-service serve", "build": "vue-cli-service build", "lint": "vue-cli-service lint" }, "dependencies": { "core-js": "^3.6.5", "vue": "^2.6.11" }, "devDependencies": { "@vue/cli-plugin-babel": "~4.5.11", "@vue/cli-plugin-eslint": "~4.5.11", "@vue/cli-service": "~4.5.11", "@vue/eslint-config-standard": "^5.1.2", "babel-eslint": "^10.1.0", "eslint": "^6.7.2", "eslint-plugin-import": "^2.20.2", "eslint-plugin-node": "^11.1.0", "eslint-plugin-promise": "^4.2.1", "eslint-plugin-standard": "^4.0.0", "eslint-plugin-vue": "^6.2.2", "lint-staged": "^9.5.0", "vue-template-compiler": "^2.6.11" }, "browserslist": [ "> 1%", "last 2 versions", "not dead" ], "gitHooks": { "pre-commit": "lint-staged" }, "lint-staged": { "*.{js,jsx,vue}": [ "vue-cli-service lint", "git add" ] }}
发布
npm publish
更新
npm publish
使用
/*1. 先安装 npm i vue-com-test-alvin -S2. 在main.js中引入// main.jsimport vueComTestAlvin from 'vue-com-test-alvin'Vue.use(vueComTestAlvin)3. 在其他组件中使用即可*/