通过npm引用的vue组件使用详解

发布时间:2020-08-23 17:15:56 作者:mrr
来源:脚本之家 阅读:191

 什么是组件:组件是Vue.js最强大的功能之一。组件可以扩展HTML元素,封装可重用的代码。在较高层面上,组件是自定义的元素,Vue.js的编译器为它添加特殊功能。在有些情况下,组件也可以是原生HTML元素的形式,以is特性扩展。

    如何注册组件?

    需要使用Vue.extend方法创建一个组件,然后使用Vue.component方法注册组件。Vue.extend方法格式如下:

var MyComponent = Vue.extend({
  // 选项...后面再介绍
})

    如果想要其他地方使用这个创建的组件,还得个组件命个名:

Vue.component('my-component', MyComponent)

   命名之后即可在HTML标签中使用这个组件名称,像使用DOM元素一样。

本文章通过实现一个vue-dialog的弹出层组件,然后附加说明如果发布此包到npm,且能被其他项目使用。

功能说明

实现

通过npm引用的vue组件使用详解

使用vue的component组件实现,他可以完美支持。

弹出层按钮支持回调

在master.vue中实现,详细解析此代码

html代码

<template>
 <div> 
  <div class="modal-content" v-for="(comp,index) in comps" v-bind: >
   <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>

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>

css代码:

<style>
.modal-content {
 position: absolute;
}
</style>

如何实用

new Vue({
 el: '#app',
 template: '<div><App></App><HDialog ref="hDialog" ></HDialog></div>',
 components: { App }
})

一定要指定ref值,这是弹出层实现的关键。

let promise = this.$root.$refs.hDialog.open({
  template: '<div>第二层了</div>'
 })
 promise.then(function (arg) {
  alert('第二层' + arg.type)
  arg.close()
})

发布到npm

output: {
path: './dist',
filename: '[name].js',
library: 'vue-hdialog',
libraryTarget: 'commonjs2'
}

以上所述是小编给大家介绍的通过npm引用的vue组件使用详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!

推荐阅读:
  1. 如何通过npm或yarn自动生成vue组件
  2. 详解如何制作并发布一个vue的组件的npm包

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue dialog 组件

上一篇:Java Process详解及实例

下一篇:vsftpd用户禁止ssh登陆的实现方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》