当前位置:   article > 正文

vue 全局自定义指令(loading)_el.instance

el.instance

注册

  1. main.js 文件中 导入封装好的 loading 方法对象
  2. 使用 Vue.directive('loading', loadingDirective) 注册全局指令

封装 Loading 指令

  1. import vue from 'vue'
  2. import loading from './loading.vue'
  3. const loadingRelative = 'loading-relative'
  4. const loadingDirective = {
  5. inserted (el, binding) {
  6. const loadingCla = vue.extend(loading) // 在vue 中创建子类
  7. // eslint-disable-next-line new-cap
  8. const instance = new loadingCla().$mount()
  9. el.instance = instance
  10. const text = binding.arg
  11. if (typeof text !== 'undefined') {
  12. instance.setTitle(text)
  13. }
  14. if (binding.value) append(el)
  15. },
  16. update (el, binding) {
  17. const text = binding.arg
  18. if (typeof text !== 'undefined') {
  19. el.instance.setTitle(text)
  20. }
  21. if (binding.value !== binding.oldValue) { // loading 节点
  22. binding.value ? append(el) : remove(el)
  23. }
  24. }
  25. }
  26. const append = (el) => {
  27. const style = getComputedStyle(el)
  28. if (!['absolute', 'fixed', 'relative'].includes(style.position)) { // loading组件
  29. 使用 position, 判断父级 position
  30. addClass(el, loadingRelative)
  31. }
  32. el.appendChild(el.instance.$el)
  33. }
  34. const remove = (el) => {
  35. removeClass(el, loadingRelative)
  36. el.removeChild(el.instance.$el)
  37. }
  38. const addClass = (el, className) => {
  39. if (!el.classList.contains(className)) {
  40. el.classList.add(className)
  41. }
  42. }
  43. const removeClass = (el, className) => {
  44. el.classList.remove(className)
  45. }
  46. export default loadingDirective

loading 组件

  1. <template lang='pug'>
  2. .loading
  3. .loading-content
  4. Icon(type="load-c" class='ip-loading-icon')
  5. .desc {{ text }}
  6. </template>
  7. <script>
  8. export default {
  9. data () {
  10. return {
  11. text: '加载中...'
  12. }
  13. },
  14. methods: {
  15. setTitle (text) {
  16. this.text = text
  17. }
  18. }
  19. }
  20. </script>
  21. <style lang='less' scoped>
  22. .loading {
  23. position: absolute;
  24. top: 50%;
  25. left: 50%;
  26. transform: translate3d(-50%, -50%, 0);
  27. .loading-content {
  28. text-align: center;
  29. color: @ip-font-color-text;;
  30. .desc {
  31. line-height: 20px;
  32. }
  33. }
  34. }
  35. .loading-relative {
  36. position: relative;
  37. }
  38. </style>

使用

  1. 在元素上 v-loading:[text]='finshed'
  2. [text] 传入 Loading title
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/218879
推荐阅读
相关标签
  

闽ICP备14008679号