当前位置:Gxlcms > html代码 > vue的本地静态图片路径

vue的本地静态图片路径

时间:2021-07-01 10:21:17 帮助过:25人阅读

这次给大家带来vue的本地静态图片路径,使用vue本地静态图片路径的注意事项有哪些,下面就是实战案例,一起来看一下。

这里写图片描述

需求:如何components里面的index.vue怎样能把assets里面的图片拿出来。

1.在img标签里面直接写上路径:

  1. <img src="../assets/a1.png" class="" width="100%"/>

2.利用数组保存再循环输出:

  1. <el-carousel-item v-for="item in carouselData" :key="item.id">
  2. <img :src="item.url" class="carouselImg"/>
  3. <span class="carouselSpan">{{ item.title }}</span>
  4. </el-carousel-item>
  5. data: () => ({
  6. carouselData:[
  7. {url:require('../assets/a1.png'),title:'你看我叼吗1',id:1},
  8. {url:require('../assets/a3.png'),title:'你看我叼吗2',id:2},
  9. {url:require('../assets/a4.png'),title:'你看我叼吗3',id:3}
  10. ]
  11. }),

效果如下:

index.vue里面的完整代码是这个:

  1. <template>
  2. <p> <p class=" block">
  3. <el-carousel class="carouselBlock">
  4. <el-carousel-item v-for="item in carouselData" :key="item.id">
  5. <img :src="item.url" class="carouselImg"/>
  6. <span class="carouselSpan">{{ item.title }}</span>
  7. </el-carousel-item>
  8. </el-carousel>
  9. </p>
  10. <footer1></footer1>
  11. <img src="../assets/a1.png" class="" width="100%"/>
  12. </p>
  13. </template>
  14. <script>
  15. import footer1 from '../components/public/footer'
  16. export default {
  17. data: () => ({
  18. carouselData:[
  19. {url:require('../assets/a1.png'),title:'你看我叼吗1',id:1},
  20. {url:require('../assets/a3.png'),title:'你看我叼吗2',id:2},
  21. {url:require('../assets/a4.png'),title:'你看我叼吗3',id:3}
  22. ]
  23. }),
  24. components:{
  25. footer1
  26. },
  27. }
  28. </script>
  29. <style lang="scss">
  30. @import '../style/mixin';
  31. .carouselBlock{
  32. width: 100%;
  33. height: REM(300);
  34. position:relative;
  35. .carouselImg{
  36. height: REM(300);
  37. width:100%;
  38. }
  39. .carouselSpan{
  40. position: absolute;
  41. bottom: REM(20);
  42. left: REM(20);
  43. font-size: REM(24);
  44. font-weight: bold;
  45. }
  46. }
  47. .el-carouselcontainer{
  48. width: 100%;
  49. height: REM(300);
  50. }
  51. .el-carouselitem h3 {
  52. color: #475669;
  53. font-size: 14px;
  54. opacity: 0.75;
  55. margin: 0;
  56. }
  57. .el-carouselitem:nth-child(2n) {
  58. background-color: #99a9bf;
  59. }
  60. .el-carouselitem:nth-child(2n+1) {
  61. background-color: #d3dce6;
  62. }
  63. </style>

PS:下面看下Vue.js中的图片引用路径

当我们在Vue.js项目中引用图片时,关于图片路径有以下几种情形:

使用一

我们在data里面定义好图片路径

imgUrl:'../assets/logo.png'

然后,在template模板里面

  1. <<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">img src="
  2. {{imgUrl}}">

这样是错误的写法,我们应该用v-bind绑定图片的srcs属性

:src="imgUrl">

或者

  1. <span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">img src="../assets/logo.png">

这种方式是按照正常HTML语法引用路径,放在模板里可以被webpack打包出来。

使用二

当我们需要在js代码里面写图片路径的时候,如果我们在data里面写

imgUrl:'../assets/logo.png'

此时webpack只会把它当做字符串处理从而找不到图片地址,此时我们可以使用import引入图片路径:

  1. :src="avatar" />
  2. import avatar from '@/assets/logo.png'

在data里面定义

avatar: avatar

情形三

我们也可以把图片放在外层的static文件夹里面,然后在data里面定义:

  1. imgUrl : '../../static/logo.png'
  2. :src="imgUrl" />

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

jQuery+koa2怎么实现Ajax请求

iview的select下拉框选项错位怎么处理

以上就是vue的本地静态图片路径的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行