Loading.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { render, h } from 'vue'
  2. import { hasOwn } from '@vue/shared'
  3. import CancelLoadingConstructor from './index.vue'
  4. function CancelLoading (options) {
  5. const container = document.createElement('div')
  6. let timeout = null
  7. options.onVanish = () => {
  8. options.onCancel && options.onCancel()
  9. clearTimeout(timeout)
  10. timeout = null
  11. render(null, container)
  12. document.body.removeChild(container)
  13. }
  14. const vnode = h(CancelLoadingConstructor, options)
  15. render(vnode, container)
  16. document.body.appendChild(container)
  17. const instance = vnode.component
  18. const vm = instance.proxy
  19. // eslint-disable-next-line no-unused-vars
  20. for (const prop in options) {
  21. if (hasOwn(options, prop) && !hasOwn(vm.$props, prop)) {
  22. vm[prop] = options[prop]
  23. }
  24. }
  25. vm.visible = true
  26. // options = {message, btnName, duration: 2000}
  27. timeout = setTimeout(() => {
  28. vm.visible = false
  29. }, options.duration || 2000)
  30. const hide = () => {
  31. vm.visible = false
  32. clearTimeout(timeout)
  33. timeout = null
  34. }
  35. return {
  36. hide
  37. }
  38. }
  39. export default CancelLoading