通過npm引用的vue組件使用詳解
什么是組件:組件是Vue.js最強大的功能之一。組件可以擴展HTML元素,封裝可重用的代碼。在較高層面上,組件是自定義的元素,Vue.js的編譯器為它添加特殊功能。在有些情況下,組件也可以是原生HTML元素的形式,以is特性擴展。
如何注冊組件?
需要使用Vue.extend方法創(chuàng)建一個組件,然后使用Vue.component方法注冊組件。Vue.extend方法格式如下:
var MyComponent = Vue.extend({
// 選項...后面再介紹
})
如果想要其他地方使用這個創(chuàng)建的組件,還得個組件命個名:
Vue.component('my-component', MyComponent)
命名之后即可在HTML標簽中使用這個組件名稱,像使用DOM元素一樣。
本文章通過實現(xiàn)一個vue-dialog的彈出層組件,然后附加說明如果發(fā)布此包到npm,且能被其他項目使用。
功能說明
- 多層彈出時,只有一個背景層。
- 彈出層嵌入內(nèi)部組件。
- 彈出層按鈕支持回調(diào)
- 源碼下載
實現(xiàn)

- 多層彈出時,只有一個背景層
- 利用兩個組件實現(xiàn),一個背景層組件(只提供一個背景層,組件名:background.vue),一個彈出層內(nèi)容管理組件(實現(xiàn)多個內(nèi)容層的管理,組件名:master.vue)。
- 彈出層嵌入內(nèi)部組件
使用vue的component組件實現(xiàn),他可以完美支持。
彈出層按鈕支持回調(diào)
在master.vue中實現(xiàn),詳細解析此代碼
html代碼
<template>
<div>
<div class="modal-content" v-for="(comp,index) in comps" v-bind:style="style(index,comp)" >
<div class="modal-header" >
header
</div>
<div class="modal-body">
<component :is="comp"></component>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" v-on:click="clickHandler(btn.value, comp, index)" v-for="btn in btns" >{{btn.text}}</button>
</div>
</div>
<hDialogBack ref="back" v-bind:z-index="realIndex-1" ></hDialogBack>
</div>
</template>
- comps:內(nèi)部組件的集合
- realIndex:一個computed屬性,讀取props的mIndex屬性,表示內(nèi)部層的zIndex層級關(guān)系。
- component加載組件
- btns:表示按鈕的集合,現(xiàn)還不支持組件獨立配置按鈕列表。
- style:此方法用于生成內(nèi)部組件居中的css代碼。
js代碼:
<script>
import hDialogBack from './background'
function getclientPoint () {
return {
width: document.documentElement.clientWidth || document.body.clientWidth,
height: document.documentElement.clientHeight || document.body.clientHeight
}
}
export default {
name: 'HDialog',
data () {
return {
comps: []
}
},
props: {
'btns': {
type: Array,
default: function () {
return [{ text: 'ok', value: 'ok'}, { text: 'cancel', value: 'cancel'}]
}
},
'mIndex': {
type: Number,
default: 19861016
}
},
computed: {
realIndex: function () {
return this.mIndex
}
},
components: {
hDialogBack
},
methods: {
open: function (comp) {
comp.promise = new Promise(function (resolve, reject) {
comp.resolve = resolve
comp.reject = reject
})
comp.width = comp.width || 600
comp.height = comp.height || 400
this.comps.push(comp)
if (!this.$refs.back.show) {
this.$refs.back.open()
}
return comp.promise
},
clickHandler: function (type, comp, index) {
let self = this
let close = function () {
self.comps.splice(index, 1)
if (self.comps.length === 0 && self.$refs.back.show) {
self.$refs.back.close()
}
}
/** 只提供promise模式 */
comp.resolve({'type': type, 'close': close})
},
style: function (index, comp) {
let point = getclientPoint()
return {
zIndex: this.realIndex + index,
width: comp.width + 'px',
height: comp.height + 'px',
left: ((point.width - comp.width) / 2) + 'px',
top: ((point.height - comp.height) / 2) + 'px'
}
}
}
}
</script>
- open方法,用于打開彈出層,且返回一個Promise。
- 嵌入background.vue組件,用于提供背景層。
- clickHandler方法:master.vue組件按鈕的事件響應(yīng)函數(shù),會resolve在open方法中提供的promise。
css代碼:
<style>
.modal-content {
position: absolute;
}
</style>
如何實用
- 首先需要在頂層引入master.vue,然后嵌入到與app組件平級,如下代碼:
new Vue({
el: '#app',
template: '<div><App></App><HDialog ref="hDialog" ></HDialog></div>',
components: { App }
})
一定要指定ref值,這是彈出層實現(xiàn)的關(guān)鍵。
- 在任意一個子組件中如下使用:
let promise = this.$root.$refs.hDialog.open({
template: '<div>第二層了</div>'
})
promise.then(function (arg) {
alert('第二層' + arg.type)
arg.close()
})
- 使用$root.$refs找到彈出層管理組件
- 使用調(diào)用其open方法,并接受一個promise類型的返回值
- 使用promise即可。
發(fā)布到npm
- 如果組件需要被其他人引用,最好使用commonjs2規(guī)范,webapck如下配置:
output: {
path: './dist',
filename: '[name].js',
library: 'vue-hdialog',
libraryTarget: 'commonjs2'
}
- 在npmjs上注冊一個賬號
- 利用npm login 登錄
- 使用npm publish 發(fā)布,如果你想卸載可以用npm unpublish --force.
- 發(fā)布是需要package.json檢測version和name字段,如果已存,或者是存在被卸載的都不行。
- package.json中的main節(jié)點是指定其他引用時,默認導(dǎo)出的文件。
以上所述是小編給大家介紹的通過npm引用的vue組件使用詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- vue組件 $children,$refs,$parent的使用詳解
- 解決vue組件中使用v-for出現(xiàn)告警問題及v for指令介紹
- Vue組件tree實現(xiàn)樹形菜單
- Vue組件之全局組件與局部組件的使用詳解
- 詳解利用jsx寫vue組件的方法示例
- 在vue組件中使用axios的方法
- vue組件如何被其他項目引用
- vue組件中點擊按鈕后修改輸入框的狀態(tài)實例代碼
- vue組件實現(xiàn)文字居中對齊的方法
- Vue組件選項props實例詳解
- vue組件中使用iframe元素的示例代碼
- 使用Vue組件實現(xiàn)一個簡單彈窗效果
- 詳解vue組件通信的三種方式
- 在Vue組件上動態(tài)添加和刪除屬性方法
- vue組件實現(xiàn)可搜索下拉框擴展
- Vue組件通信之Bus的具體使用
- Vue組件BootPage實現(xiàn)簡單的分頁功能
- 從零開始封裝自己的自定義Vue組件
相關(guān)文章
vue鼠標移入添加class樣式,鼠標移出去除樣式(active)實現(xiàn)方法
今天小編就為大家分享一篇vue鼠標移入添加class樣式,鼠標移出去除樣式(active)實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
使用Vue+Django+Ant Design做一個留言評論模塊的示例代碼
這篇文章主要介紹了使用Vue+Django+Ant Design做一個留言評論模塊,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06
Vue this.$router.push(參數(shù))實現(xiàn)頁面跳轉(zhuǎn)操作
這篇文章主要介紹了Vue this.$router.push(參數(shù))實現(xiàn)頁面跳轉(zhuǎn)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
vue.js中Vue-router 2.0基礎(chǔ)實踐教程
這篇文章主要給大家介紹了關(guān)于vue.js中Vue-router 2.0基礎(chǔ)實踐的相關(guān)資料,其中包括vue-router 2.0的基礎(chǔ)用法、動態(tài)路由匹配、嵌套路由、編程式路由、命名路由以及命名視圖等相關(guān)知識,需要的朋友可以參考借鑒,下面來一起看看吧。2017-05-05

