index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // 当目标变量为空时显示空文案
  2. const emptyChange = (el, binding, vnode) => {
  3. // binding.value为文本 配合v-html 使用
  4. if (typeof binding.value === 'string') {
  5. if (!vnode.data.domProps.innerHTML.trim()) {
  6. el.classList.add('gray-color')
  7. el.innerHTML = binding.value
  8. } else {
  9. el.classList.remove('gray-color')
  10. }
  11. return
  12. }
  13. // binding.value为对象{open:true,text:'空文案'} 配合v-model 使用
  14. vnode.child.$nextTick(() => {
  15. let isEmpty = false
  16. if (binding.value.open) {
  17. if (!vnode.data.model.value) {
  18. isEmpty = true
  19. }
  20. }
  21. if (isEmpty) {
  22. // console.log('hide-child', el.classList.contains('hide-child'))
  23. if (!el.classList.contains('hide-child')) {
  24. el.classList.add('hide-child', 'gray-color')
  25. el.appendChild(document.createTextNode(binding.value.text))
  26. }
  27. } else {
  28. if (el.classList.contains('hide-child')) {
  29. el.classList.remove('hide-child', 'gray-color')
  30. el.removeChild(el.lastChild)
  31. }
  32. }
  33. })
  34. }
  35. const empty = {
  36. bind: emptyChange,
  37. update: emptyChange
  38. }
  39. export { empty }