经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
你不知道的Vue技巧之--开发一个可以通过方法调用的组件(推荐)
来源:jb51  时间:2019/4/16 8:34:48  对本文有异议

Vue作为最近最炙手可热的前端框架,其简单的入门方式和功能强大的API是其优点。而同时因为其API的多样性和丰富性,所以他的很多开发方式就和一切基于组件的React不同,如果没有对Vue的API(有一些甚至文档都没提到)有一个全面的了解,那么在开发和设计一个组件的时候有可能就会绕一个大圈子,所以我非常推荐各位在学习Vue的时候先要对Vue核心的所有API都有一个了解。

举个例子,通知组件notification基本是现代web开发标配,在很多地方都能用到。而在以Vue作为核心框架的前端项目中,因为Vue本身是一个组件化和虚拟Dom的框架,要实现一个通知组件的展示当然是非常简单的。但因为通知组件的使用特性,直接在模板当中书写组件并通过v-show或者props控制通知组件的显示显然是非常不方便的,而且如果要在action或者其他非组件场景中要用到通知,那么纯组件模式的用法也无法实现。那么有没有办法即用到Vue组件化特性方便得实现一个通知组件的展现,又能够通过一个简单的方法调用就能显示通知呢?本文就是来讲述这个实现方法的。

目标

实现一个Vue的通知组件,可以直接在组件内调用
通过方法调用,比如Vue.$notify({...options})来调用通知组件
结合上述两种方式,复用代码

实现通知组件

这一步非常的简单,我相信做过一点Vue开发的同学都能写出一个像模像样的通知组件,在这里就不赘述,直接上代码

  1. <template>
  2. <transition name="fade" @after-leave="afterLeave" @after-enter="setHeight">
  3. <div
  4. v-show="visible"
  5. :class="['notification']"
  6. :style="style"
  7. @mouseenter="clearTimer"
  8. @mouseleave="createTimer"
  9. >
  10. <span class="content">{{content}}</span>
  11. <a class="btn" @click="handleClose">{{btn || '关闭'}}</a>
  12. </div>
  13. </transition>
  14. </template>
  15.  
  16. <script>
  17. export default {
  18. name: 'Notification',
  19. props: {
  20. content: {
  21. type: String,
  22. default: ''
  23. },
  24. btn: {
  25. type: String,
  26. default: ''
  27. }
  28. },
  29. data () {
  30. return {
  31. visible: true
  32. }
  33. },
  34. computed: {
  35. style () {
  36. return {}
  37. }
  38. },
  39. methods: {
  40. handleClose (e) {
  41. e.preventDefault()
  42. this.doClose()
  43. },
  44. doClose () {
  45. this.visible = false
  46. this.$emit('close')
  47. },
  48. afterLeave () {
  49. this.$emit('closed')
  50. },
  51. clearTimer () {},
  52. createTimer () {},
  53. setHeight () {}
  54. }
  55. }
  56. </script>
  57.  
  58.  
  1. <style lang="stylus" scoped>
  2. .notification
  3. display: flex
  4. background-color #303030
  5. color rgba(255, 255, 255, 1)
  6. align-items center
  7. padding 20px
  8. position fixed
  9. min-width 280px
  10. box-shadow 0px 3px 5px -1px rgba(0, 0, 0, 0.2), 0px 6px 10px 0px rgba(0, 0, 0, 0.14), 0px 1px 18px 0px rgba(0, 0, 0, 0.12)
  11. flex-wrap wrap
  12. transition all .3s
  13. .content
  14. padding 0
  15. .btn
  16. color #ff4081
  17. padding-left 24px
  18. margin-left auto
  19. cursor pointer
  20. </style>

在这里需要注意,我们定义了一个叫做style的computed属性,三个方法clearTimer,createTimer,setHeight,但他们的内容都是空的,虽然在模板上有用到,但是似乎没什么意义,在后面我们要扩展组件的时候我会讲到为什么要这么做。

创建完这个组件之后,我们就可以在模板中使用了<notification btn="xxx" content="xxx" />

实现通过方法调用该通知组件

继承组件

在实现通过方法调用之前,我们需要扩展一下这个组件,因为仅仅这些属性,并不够我们使用。在使用方法调用的时候,我们需要考虑一下几个问题:

  1. 显示通知的定位
  2. 组件的出现和自动消失控制
  3. 连续多次调用通知方法,如何排版多个通知

在这个前提下,我们需要扩展该组件,但是扩展的这些属性不能直接放在原组件内,因为这些可能会影响组件在模板内的使用,那怎么办呢?这时候我们就要用到Vue里面非常好用的一个API,extend,通过他去继承原组件的属性并扩展他。

我们先来看代码,创建一个叫做fun-notification.js的文件,内容如下:

  1. import Notification from './notification.vue'
  2.  
  3. export default {
  4. extends: Notification,
  5. computed: {
  6. style () {
  7. return {
  8. position: 'fixed',
  9. right: '20px',
  10. bottom: `${this.verticalOffset + 20}px`
  11. }
  12. }
  13. },
  14. data () {
  15. return {
  16. verticalOffset: 0,
  17. visible: false,
  18. height: 0,
  19. autoClose: 3000
  20. }
  21. },
  22. mounted () {
  23. this.createTimer()
  24. },
  25. methods: {
  26. createTimer () {
  27. if (this.autoClose) {
  28. this.timer = setTimeout(() => {
  29. this.doClose()
  30. }, this.autoClose)
  31. }
  32. },
  33. clearTimer () {
  34. if (this.timer) {
  35. clearTimeout(this.timer)
  36. }
  37. },
  38. setHeight () {
  39. this.height = this.$el.offsetHeight
  40. }
  41. }
  42. }
  43.  

我们可以看到之前空实现的几个方法在这里被实现了,那么为什么要在原组件上面加上那些方法的定义呢?因为需要在模板上绑定,而模板是无法extend的,只能覆盖,如果要覆盖重新实现,那扩展的意义就不是很大了。当然同学们可以自己抉择。

在使用extend的时候注意以下两个点:

  1. 方法和属性的定义是直接覆盖的
  2. 生命周期方法类似余mixin,会合并,也就是原组件和继承之后的组件都会被调用,原组件先调用

通过方法调用该组件

最后我们需要做的就是通过方法调用这个已经继承过的组件了,我们先来看一下源码的实现:

  1. // function-component.js
  2. import Vue from 'vue'
  3. import Component from './fun-component'
  4. const NotificationConstructor = Vue.extend(Component)
  5.  
  6. const instances = []
  7. let seed = 1
  8.  
  9. const removeInstance = (instance) => {
  10. const len = instances.length
  11. if (!instance) return
  12. const index = instances.findIndex(inst => instance.id === inst.id)
  13.  
  14. instances.splice(index, 1)
  15.  
  16. if (len <= 1) return
  17. const removedHeight = instance.vm.height
  18. for (let i = index; i < len - 1; i++) {
  19. instances[i].verticalOffset =
  20. parseInt(instances[i].verticalOffset) - removedHeight - 16
  21. }
  22. }
  23.  
  24. const notify = function (options) {
  25. const {
  26. onClose,
  27. ...rest
  28. } = options
  29. if (Vue.prototype.$isServer) return
  30. options = options || {}
  31. const id = `notification_${seed++}`
  32.  
  33. const instance = new NotificationConstructor({
  34. propsData: {
  35. ...rest
  36. }
  37. })
  38.  
  39. instance.id = id
  40. instance.vm = instance.$mount()
  41. document.body.appendChild(instance.vm.$el)
  42. instance.vm.visible = true
  43.  
  44. let verticalOffset = 0
  45. instances.forEach(item => {
  46. verticalOffset += item.$el.offsetHeight + 16
  47. })
  48. verticalOffset += 16
  49. instance.verticalOffset = verticalOffset
  50. instances.push(instance)
  51. instance.vm.$on('closed', () => {
  52. if (typeof onClose === 'function') {
  53. onClose(instance)
  54. }
  55. removeInstance(instance)
  56. instance.vm.$destroy()
  57. })
  58. return instance.vm
  59. }
  60.  
  61. export default notify
  62.  

首先通过const NotificationConstructor = Vue.extend(Component),我们得到了一个类似于Vue的子类,我们就可以通过new NotificationConstructor({...options})的方式去创建Vue的实例了,同时通过该方式创建的实例,是有组件定义里面的所有属性的。

在创建实例之后,可以通过instance.$mount()手动将组件挂载到DOM上面,这样我们可以不依赖Vue组件树来输出DOM片段,达到自由显示通知的效果。

这中间的实现主要就是维护一个通知数组,在创建时推入,在消失时删除,这个过程并没有规定一定要如此实现,我就不赘述,以免限制大家的思路,大家可以根据自己的想法去实现。

使用该方法

要使用这个通知方法非常简单,我们可以直接import这个文件来使用,比如:

  1. import notify from './function-component.js'
  2.  
  3. notify({
  4. content: 'xxx',
  5. btn: 'xxx'
  6. })
  7.  

当然我们很多场景是在组件内部调用,为了方便在组件内使用,不需要每次都import,我们可以把这个方法包装成一个Vue的插件。我们创建一个index.js,内容如下:

  1. import Notification from './notification.vue'
  2. import notify from './function'
  3.  
  4. export default (Vue) => {
  5. Vue.component(Notification.name, Notification)
  6. Vue.prototype.$notify = notify
  7. Vue.notify = notify
  8. }
  9.  

然后在项目内,我们可以通过:

  1. import notify from '/path/to/notification/module'
  2.  
  3. Vue.use(notify)
  4.  

这样之后,在组件内就可以直接通过this.$notify({...options})来调用通知了,同时还可以通过Vue.notify({...options})在其他环境下调用,大家可以在自己的项目中尝试一下。

总结

到这里,关于如何实现通过方法调用一个Vue组件内容就差不多了。在这里我们涉及到的Vue技术点有如下几点:

  1. 通过extend配置进行组件的扩展
  2. 通过Vue.extend创建一个Vue的子类,用来动态创建Vue实例
  3. 通过Vue实例主动将组件内容挂载到DOM

Vue拥有非常多的API,如果在使用Vue之前没有系统的学习过Vue的核心知识和API,你可能压根就不知道有这样的实现方式,所以想要学好Vue,系统得对Vue的核心进行学习是非常重要的一个环节。

以上所述是小编给大家介绍的你不知道的Vue技巧之--开发一个可以通过方法调用的组件详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对w3xue网站的支持!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号