src_views_followOrder_myFollow_index_vue.js:903 Uncaught (in promise) Error: Cannot find module './undefined-icon.svg'

vue中img图片资源动态导入,在数据没有过来的时候报错了-解决方案_ico

vue中img图片资源动态导入,在数据没有过来的时候报错了-解决方案_ico_02

这个报错的原因是 scope.row.exchange 的值为 undefined,导致动态导入图片的路径出现了问题。可以通过给路径加上一个默认值来解决这个报错:

:src="require(`@/assets/images/followOrder/${scope.row?.exchange?.toLowerCase() || 'default'}-icon.svg`)"


<img
  v-if="scope.row?.exchange"
  :src="
  require(`@/assets/images/followOrder/${
  scope.row?.exchange?.toLowerCase() || 'default'
  }-icon.svg`)
  "
/>

这样,如果 scope.row.exchange 的值为 undefined,就会加载默认的图片。

注意:

方式一

default-icon.svg  本地路径要有这个命名的图片

方式二

使用v-if方式