index.ts 833 B

1234567891011121314151617181920212223242526272829303132
  1. import type { App, Directive } from 'vue'
  2. import { convertImageUrl } from '@/utils/image-helper'
  3. /**
  4. * 图片URL处理指令
  5. * 使用方法:v-image="imageUrl"
  6. * 或者:v-image:[属性名]="imageUrl" 例如 v-image:src="imageUrl"
  7. */
  8. export const imageDirective: Directive = {
  9. mounted(el, binding) {
  10. const attributeName = binding.arg || 'src'
  11. const url = binding.value
  12. if (url) {
  13. const convertedUrl = convertImageUrl(url)
  14. el.setAttribute(attributeName, convertedUrl)
  15. }
  16. },
  17. updated(el, binding) {
  18. const attributeName = binding.arg || 'src'
  19. const url = binding.value
  20. if (url) {
  21. const convertedUrl = convertImageUrl(url)
  22. el.setAttribute(attributeName, convertedUrl)
  23. }
  24. }
  25. }
  26. export function setupImageDirective(app: App) {
  27. app.directive('image', imageDirective)
  28. }