当前位置:   article > 正文

vite中img标签动态src无法正常显示_vite img src

vite img src

vite中动态拼接src地址
1.用相对路径的写法在<img>标签上是可以显示的
        <img src="../../assets/rose/rose-1.jpg" alt="" />
2.但是会用动态传值之后,图片显示不了
        <el-image :src="item.src"  />
原因是因为动态地址,路径被加载器解析为字符串,所以图片找不到
解决办法:
(1)import每张图片,使用的时候直接拿去用
       

  1. import rose1 from "../../assets/rose/rose-1.jpg"
  2. import rose2 from "../../assets/rose/rose-2.jpg"
  3. const divArray = reactive([
  4. {
  5. id: 1,
  6. src: rose1 ,
  7. style_d: "--d:1",
  8. span_name: "花朵",
  9. a_name: "五颜六色玫瑰花"
  10. },
  11. {
  12. id: 2,
  13. src: rose2 ,
  14. style_d: "--d:2",
  15. span_name: "花朵",
  16. a_name: "白色雏菊"
  17. }
  18. }


但是每张图片都要import,非常笨重
(2)参考vite官方的静态资源处理文档

静态资源处理 | Vite 官方中文文档
使用import.meta.url 是一个 ESM 的原生功能,会暴露当前模块的 URL。将它与原生的 URL 构造器 组合使用,在一个 JavaScript 模块中,通过相对路径我们就能得到一个被完整解析的静态资源


创建一个工具类getImageByPath.ts

  1. /* 将静态资源的图片导入vue中 */
  2. const getImageUrl:any = (name:any) => new URL(`../${name}`, import.meta.url).href
  3. export default getImageUrl


组件中使用:

  1. import getImageUrl from "../../utils/getImageByPath"
  2. ...
  3. //拼接图片路径
  4. const concatPath = (id: number) => {
  5.   return getImageUrl("assets/rose/rose-" + id + ".jpg")
  6. }
  7. ...
  8. const divArray = reactive([
  9. {
  10. id: 1,
  11. src: concatPath(1),
  12. style_d: "--d:1",
  13. span_name: "花朵",
  14. a_name: "五颜六色玫瑰花"
  15. },
  16. {
  17. id: 2,
  18. src: concatPath(2),
  19. style_d: "--d:2",
  20. span_name: "花朵",
  21. a_name: "白色雏菊"
  22. }
  23. }

完整代码附上:

图片存放的地址不同,concatPath中拼接的url也不同

  1. <template>
  2. <div class="body">
  3. <template v-for="item in divArray" :key="item.id">
  4. <div class="container">
  5. <div class="card" :style="item.style_d">
  6. <div class="content">
  7. <div class="img">
  8. <el-image :src="item.src" class="img_content" />
  9. </div>
  10. <div class="detail">
  11. <span>{{ item.span_name }}</span>
  12. <p>{{ item.a_name }}</p>
  13. </div>
  14. </div>
  15. <a href="#" class="a">关注</a>
  16. </div>
  17. </div>
  18. </template>
  19. </div>
  20. </template>
  21. <script setup lang="ts" name="cardSlide">
  22. import { ref, reactive } from "vue"
  23. import getImageUrl from "../../utils/getImageByPath"
  24. defineProps()
  25. //拼接图片路径
  26. const concatPath = (id: number) => {
  27. return getImageUrl("assets/rose/rose-" + id + ".jpg")
  28. }
  29. const divArray = reactive([
  30. {
  31. id: 1,
  32. src: concatPath(1),
  33. style_d: "--d:1",
  34. span_name: "花朵",
  35. a_name: "五颜六色玫瑰花"
  36. },
  37. {
  38. id: 2,
  39. src: concatPath(2),
  40. style_d: "--d:2",
  41. span_name: "花朵",
  42. a_name: "白色雏菊"
  43. },
  44. {
  45. id: 3,
  46. src: concatPath(3),
  47. style_d: "--d:3",
  48. span_name: "花朵",
  49. a_name: "蓝玫瑰"
  50. },
  51. {
  52. id: 4,
  53. src: concatPath(4),
  54. style_d: "--d:4",
  55. span_name: "花朵",
  56. a_name: "粉色雏菊"
  57. },
  58. {
  59. id: 5,
  60. src: concatPath(5),
  61. style_d: "--d:5",
  62. span_name: "花朵",
  63. a_name: "五叶草"
  64. }
  65. ])
  66. </script>
  67. <style lang="less" scoped>
  68. // div {
  69. // width: 750px;
  70. // height: 500px;
  71. // // border: 1px solid black;
  72. // background-image: url(../../assets/rose/flower-1.png);
  73. // background-size: 100%;
  74. // background-repeat: no-repeat;
  75. // }
  76. .body {
  77. height: 100vh;
  78. display: flex;
  79. justify-content: center;
  80. align-items: center;
  81. background: linear-gradient(200deg, #b1f4cf, #9890e3);
  82. }
  83. .container {
  84. display: flex;
  85. justify-content: center;
  86. align-items: center;
  87. width: 500px;
  88. height: 300px;
  89. }
  90. .container:hover .card {
  91. animation-play-state: paused;
  92. }
  93. .card {
  94. position: absolute;
  95. background-color: #fff;
  96. width: 430px;
  97. height: 100px;
  98. display: flex;
  99. justify-content: space-between;
  100. align-items: center;
  101. padding: 0 20px;
  102. border-radius: 100px 20px 20px 100px;
  103. opacity: 0;
  104. animation: animate 5s linear infinite;
  105. animation-delay: calc(1.1s * var(--d));
  106. .content {
  107. display: flex;
  108. align-items: center;
  109. .img {
  110. width: 90px;
  111. height: 90px;
  112. position: absolute;
  113. left: 0;
  114. top: 0;
  115. background-color: #fff;
  116. padding: 5px;
  117. border-radius: 50%;
  118. box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
  119. }
  120. .detail {
  121. margin-left: 100px;
  122. span {
  123. display: block;
  124. font-size: 18px;
  125. font-weight: 600;
  126. margin-bottom: 8px;
  127. }
  128. }
  129. }
  130. .img img_content {
  131. width: 100%;
  132. height: 100%;
  133. object-fit: cover;
  134. object-position: 50% 50%;
  135. border-radius: 50%;
  136. }
  137. .a {
  138. font-size: 14px;
  139. text-decoration: none;
  140. background: linear-gradient(to bottom, #fbc5ed, #a6c1ee);
  141. padding: 7px 18px;
  142. color: #fff;
  143. border-radius: 25px;
  144. }
  145. }
  146. @keyframes animate {
  147. 0% {
  148. opacity: 0;
  149. transform: translateY(100%) scale(0.5);
  150. }
  151. 5%,
  152. 20% {
  153. opacity: 0.4;
  154. transform: translateY(100%) scale(0.7);
  155. }
  156. 25%,
  157. 40% {
  158. opacity: 1;
  159. transform: translateY(0) scale(1);
  160. }
  161. 45%,
  162. 60% {
  163. opacity: 0.4;
  164. transform: translateY(-100%) scale(0.7);
  165. }
  166. 100% {
  167. opacity: 0;
  168. transform: translateY(-100%) scale(0.5);
  169. }
  170. }
  171. </style>



 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/338758
推荐阅读
相关标签
  

闽ICP备14008679号