請先確保安裝了nodejs,vue
html
全局安裝npm install -g @vue/clivue
Vue CLI 的包名稱由 vue-cli 改爲了 @vue/cli。 若是你已經全局安裝了舊版本的 vue-cli (1.x 或 2.x),你須要先經過 npm uninstall vue-cli -g 或 yarn global remove vue-cli 卸載它。node
vue -V 查看是否安裝成功 webpack
若報錯:bash: vue: command not found ;是由於沒找到按照的vue,到安裝目錄下運行vue -V,查看是否安裝成功,若成功,可是其餘目錄還報錯,請查找一下環境變量是否配置正確;若仍是沒法解決請卸載node,vue,vue-cli從新安裝ios
{
"name": "vue-short-cut",//包名,該名不能和已有的名稱衝突;
"version":"0.1.0", //版本號,不能和歷史版本號相同;
"description":"使用vue的directive來快捷獲取焦點,支持回車、方向鍵、回車+方向鍵",// 簡介;
"main":"lib/vue-short-cut.umd.min.js", //入口文件,應指向編譯後的包文件;
"keyword":"vue directive short cut focus",//關鍵字,以空格分割;
"author":"logmei",//做者;
"private":false,//是否私有,須要修改成 false 才能發佈到 npm;
"license":"MIT",//開源協議。
...
}
複製代碼
構建目標官方文檔git
{
"scripts":{
"lib": "vue-cli-service build --target lib --name vue-short-cut --dest lib packages/index.js"
}
}
複製代碼
"repository": {
"type": "git",
"url": "https://github.com/axios/axios.git"
}
複製代碼
若組件有依賴,則正常安裝npm i element-ui -D,保存到開發依賴就能夠了(.vue文件中必須添加name) github
建立插件文件index.js
import SearchComponent from './src'
SearchComponent.install = Vue => Vue.component(SearchComponent.name,SearchComponent)
export default SearchComponent
複製代碼
若是有更多的插件,能夠繼續添加,文件目錄爲web
建立完成以後,須要建立構建時的入口文件packages/index.js,實現組件全局註冊vuex
import CommonDialog from './CommonDialog/src'
import Search from './Search/src'
const components = [
CommonDialog,
Search
]
const install = function(Vue){
if(install.installed) return
install.installed = true
components.map(component => {
Vue.component(component.name,component)
})
}
/** 支持使用標籤方式引入 */
if(typeof window != 'undefined' && window.Vue){
install(window.Vue)
}
export default {
install,
...components
}
複製代碼
import KeyDownKey from './common/keydown.js'
const shortCut = {
inserted: function(el, binding, vnode) {
switch (binding.arg) {
case 'keydown' :
setTimeout(function() {
if (binding.modifiers.enter) (KeyDownKey.enter)(el, binding, vnode)
if (binding.modifiers.arrow) (KeyDownKey.arrow)(el, binding, vnode)
if (binding.modifiers.keyDown) (KeyDownKey.keyDown)(el, binding, vnode)
}, 0)
break
}
},
componentUpdated: function(el, binding, vnode) {
switch (binding.arg) {
case 'keydown' :
setTimeout(function() {
if (binding.modifiers.enter) (KeyDownKey.enter)(el, binding, vnode)
if (binding.modifiers.arrow) (KeyDownKey.arrow)(el, binding, vnode)
if (binding.modifiers.keyDown) (KeyDownKey.keyDown)(el, binding, vnode)
}, 0)
break
}
}
}
const install = function(Vue) {
Vue.directive('shortCut', shortCut)
}
shortCut.install = install
export default shortCut
複製代碼
cli3提供了build指令vue-cli
"lib": "vue-cli-service build --target lib --name vue-short-cut --dest lib packages/directives/index.js"
複製代碼
/dist
/lib
/src
/packages
/public
vue.config.js
babel.config.js
*.map
*.html
複製代碼
發佈時會遇到的問題
npm config set registry https://registry.npm.taobao.org
,但發佈npm包時必須爲npm的地址:npm config set registry http://registry.npmjs.org/
npm ERR! code E403
npm ERR! 403 Forbidden - PUT https://registry.npm.taobao.org/vue-short-cut - [no_perms] Private mode enable, only admin can publish this module
複製代碼
npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! You do not have permission to publish "vue-short-cut". Are you logged in as the correct user?
複製代碼
npm publish --access=public
npm ERR! code E403
npm ERR! 403 Forbidden - PUT http://registry.npmjs.org/vue-short-cut - Package name too similar to existing packages; try renaming your package to '@logmei/vue-short-cut' and publishing with 'npm publish --access=public' instead
複製代碼
npm ERR! code E403
npm ERR! 403 Forbidden - PUT http://registry.npmjs.org/@logmei%2fvue-short-cut - You cannot publish over the previously published versions: 0.1.2.
複製代碼
根據規範,只有在發包的24小時內才容許撤銷發佈的包( unpublish is only allowed with versions published in the last 24 hours); 即便你撤銷了發佈的包,發包的時候也不能再和被撤銷的包的名稱和版本重複了(即不能名稱相同,版本相同,由於這二者構成的惟一標識已經被「佔用」了)
npm i @logmei/vue-short-cut -D
import VueShortCut from '@logmei/vue-short-cut'
Vue.use(VueShortCut)
複製代碼