小編給大家分享一下vue封裝第三方插件并發(fā)布到npm的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
成都創(chuàng)新互聯(lián)公司是專業(yè)的遂川網(wǎng)站建設(shè)公司,遂川接單;提供成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行遂川網(wǎng)站開發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
gitment
gitment是一個(gè)基于github issues封裝的評(píng)論插件,以這個(gè)插件作為演示,把它封裝成vue插件。vue-gitment,該插件已發(fā)布到npm,并在自己的開源項(xiàng)目vueblog中安裝使用
項(xiàng)目初始化
封裝vue的插件用webpack-simple很合適,vue init webpack-simple vue-gitment此命令創(chuàng)建我們的項(xiàng)目的目錄,創(chuàng)建文件夾和文件,最后結(jié)構(gòu)是這樣的

lib目錄是我們的插件目錄,其他的默認(rèn)就好
修改配置項(xiàng)
首先是修改package.json
{
"name": "vue-gitment",
"version": "0.1.1",
"description": "A comment plugin by gitment",
"main": "dist/vue-gitment.js",
"directories": {
"dist": "dist"
},
"scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
},
"repository": {
"type": "git",
"url": "git+https://github.com/vue-blog/vue-gitment.git"
},
"dependencies": {
"gitment": "^0.0.3",
"vue": "^2.3.3"
},
"devDependencies": {
},
"author": "wmui",
"license": "MIT",
"bugs": {
"url": "https://github.com/vue-blog/vue-gitment/issues"
},
"homepage": "https://github.com/vue-blog/vue-gitment#readme"
}把依賴性gitment添加到dependencies,main是我們打包后的文件入口,你可以用npm init命令生成一個(gè)package.json
修改webpack.config.js

我們只需配置入口和出口,不要?jiǎng)h除默認(rèn)的配置,因?yàn)楹竺骈_發(fā)好插件,我們需要查看工作效果
修改index.html

因?yàn)槲覀冃薷牧藈ebpack配置,自然要把script的src修改一下
封裝插件
VueComment.vue內(nèi)容如下
<template>
<div v-comment="options"></div>
</template>
<script>
// 引入依賴項(xiàng)
import Gitment from 'gitment'
export default {
name: 'vue-comment',
props: ['options'],
directives: {
// 自定義指令
comment: {
bind: function (el, binding) {
const gitment = new Gitment({
id: binding.value.id + '',
owner: binding.value.owner,
repo: binding.value.repo,
oauth: {
client_id: binding.value.oauth.client_id,
client_secret: binding.value.oauth.client_secret
}
})
gitment.render(el)
}
}
}
}
</script>相信熟悉vue的一眼都看懂了,render函數(shù)是gitment對(duì)象的方法,不用關(guān)心,和我們開發(fā)組件是一樣一樣的
index.js封裝組件
import VueComment from './VueComment.vue'
const comment = {
install: function(Vue) {
Vue.component(VueComment.name, VueComment)
}
}
// 這里的判斷很重要
if (typeof window !== 'undefined' && window.Vue) {
window.Vue.use(comment)
}
export default comment我們?cè)趙ebpack配置的入口文件就是他,install是掛載組件的方法,有了它我們就可以在外部use一個(gè)插件了,簡(jiǎn)單吧
測(cè)試插件
首先測(cè)試build是否成功
npm run builddist目錄會(huì)生成如下文件

可喜可賀,接下來(lái)測(cè)試插件是否正常工作
我們需要把package和webpack的修改一下,這就是為什么我前面說(shuō)不要?jiǎng)h除而是注釋掉 ,把package.json的main修改為dist/build.js,wepack的entry和filename換成默認(rèn)配置,index.html的src也換成默認(rèn)的
在main.js中引入我們的組件
import VueComment from './lib/index.js' Vue.use(VueComment)
App.vue中使用我們的插件
<template>
<div id="app">
<vue-comment :options="options" v-if="options"></vue-comment>
</div>
</template>
<script>
export default {
name: 'App',
data() {
return {
options: {
id: 'article id',
owner: 'Your GitHub ID',
repo: 'The repo to store comments',
oauth: {
client_id: 'Your client ID',
client_secret: 'Your client secret',
}
}
}
}
}
</script>
<style>
@import '~gitment/style/default.css';
</style>執(zhí)行npm run dev

哈哈,它正常工作了,Error: Not Found是因?yàn)槲覜]配置client_id。
發(fā)布插件
完成測(cè)試工作后我們就可以發(fā)布到npm了,這個(gè)就比較見到了,注冊(cè)個(gè)npm賬號(hào),在你要發(fā)布的項(xiàng)目目錄執(zhí)行npm login,輸入賬號(hào)密碼和郵箱,然后npm publish就發(fā)布成功了,npm install vue-gitment查看效果,建議直接看源代碼,因?yàn)檎娴暮芎?jiǎn)單。
以上是“vue封裝第三方插件并發(fā)布到npm的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享題目:vue封裝第三方插件并發(fā)布到npm的示例分析
文章分享:http://chinadenli.net/article30/goehso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)站營(yíng)銷、電子商務(wù)、品牌網(wǎng)站制作、做網(wǎng)站、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)