一個vue組件庫發(fā)布到npm的完整實現(xiàn)過程
新建項目
初始化一個vue項目
src下面創(chuàng)建一個plugins文件夾,index.js文件,這個文件是組件的出口文件。
npm install @/vue-cli -g npm create vue-components cd vue-components npm run serve
創(chuàng)建組件庫
src下新建一個plugins文件夾
然后創(chuàng)建toast組件toast/index.vue
這里為了能夠展示模版,div標簽后面的>去掉了。
<template> <div class="hello" <div class="toast" :class="{active: toastStatus}" <div class="toast-wrapper"{{text}}</div </div </div </template> <script> export default { name: 'vue-toast', data() { return { toastStatus: false, text:'' } }, methods: { handlerToast(toastInfo,time) { this.text = toastInfo; this.toastStatus = true; time = time || 2000 setTimeout(() => { this.toastStatus = false; }, time) } }, } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped > .toast { position: absolute; left: 50%; top: 30%; transform: translate(-50%, 50%); min-width: 0px; min-height: 0; table-layout: center; background: rgba(0,0,0,0.5); display: none } .active { display: block } </style>
再創(chuàng)建一個button組件button/index.vue
<template> <div class="hello" <button這里是button組件</button </div </template> <script> export default { name: 'vue-button', props: { msg: String }, data() { return { } }, methods: { }, } </script>
兩個組件都是很簡單的組件,這里對于組件的內(nèi)容不做詳細解釋,我們要達到這樣的效果,如下面的vue的入口文件main.js。只需要引入plugins,然后Vue.use()一下就可以在全局使用toast組件和button組件。
所以這兩個組件一定是全局注冊的,注冊的過程就在Vue.use()調(diào)用的過程中。
import Vue from 'vue' import App from './App.vue' import router from './router' import store from './store' import './registerServiceWorker' import plugins from './plugins' Vue.config.productionTip = false Vue.mixin({ mounted() { console.log('組件加載完成') } }) Vue.use(plugins) new Vue({ router, store, render: h => h(App) }).$mount('#app')
如果要實現(xiàn)上面效果,就需要在plugins中創(chuàng)建一個index.js文件,這個文件需要暴露一個對象,對象中有install方法。
import Toast from './toast' import Button from './button' const install = (Vue) => { console.log(Vue, Toast, Button) } export default { install };
在這個方法中,接收到一個參數(shù)是Vue,我們就可以在這個方法中把所有需要的組件注冊好。
import Toast from './toast' import Button from './button' const install = (Vue) => { console.log(Vue, Toast, Button); Vue.component(Toast.name, Toast); Vue.component(Button.name, Toast); } export default { install };
這樣就可以在在任何一個組件中使用你寫好的組件了。
來試一下, 把 HelloWorld
組件替換為vue-button。
可以看到組件已經(jīng)可以正常展示了。并且可以在任意的地方使用。
如果plugins里面組件比較多,就需要寫很多引入的代碼,然后再一個一個的去注冊,寫起來有是一件無腦化的事情。
這里可以借用require這個api做到自動化引入。
plugins/index.js
const requireComponent = require.context('./', true, /\.vue$/); console.log(requireComponent.keys()) const install = (Vue) => { if(install.installed) return; install.installed = true; requireComponent.keys().map(path => { const config = requireComponent(path); console.log(config) const componnetName = config.default.name; Vue.component(componnetName, config.default || config) }) } export default { install };
就這么看可能不知道在做什么,來看下下面兩個值,你就能明白上面代碼在做什么了。
requireComponent.keys()剛好是文件的路徑
console.log(requireComponent.keys())
requireComponent(path)的返回值,正好包含了組件的實例信息
console.log(config)
打包部署
接下來對項目打包發(fā)布。
調(diào)整package.json文件 scripts
"lib": "vue-cli-service build --target lib --name vue-toast --dest lib src/plugins/index.js"
npm run lib
生產(chǎn)打包目錄
package.json文件這幾個屬性不可缺少
登陸npm
npm publish發(fā)布
使用
npm i vue-toast-maile
import plugins from 'vue-toast-maile' import "../node_modules/vue-toast-maile/lib/vue-toast.css" Vue.use(plugins)
就可以在項目中使用了。
總結(jié)
到此這篇關(guān)于一個vue組件庫發(fā)布到npm的文章就介紹到這了,更多相關(guān)vue組件庫發(fā)布npm內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解vue.js數(shù)據(jù)傳遞以及數(shù)據(jù)分發(fā)slot
本篇文章給大家通過代碼實例分析了vue.js數(shù)據(jù)傳遞以及數(shù)據(jù)分發(fā)slot的相關(guān)知識,有這方面興趣的朋友參考下吧。2018-01-01詳解探索 vuex 2.0 以及使用 vuejs 2.0 + vuex 2.0 構(gòu)建記事本應(yīng)用
本篇文章主要介紹了詳解探索 vuex 2.0 以及使用 vuejs 2.0 + vuex 2.0 構(gòu)建記事本應(yīng)用 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06