国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

vue中本地靜態圖片路徑寫法

2019-11-19 14:13:39
字體:
來源:轉載
供稿:網友

這里寫圖片描述

需求:如何components里面的index.vue怎樣能把assets里面的圖片拿出來。

1.在img標簽里面直接寫上路徑:

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

2.利用數組保存再循環輸出:

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

效果如下:

index.vue里面的完整代碼是這個:

<template> <div>  <div class=" block">  <el-carousel class="carouselBlock">   <el-carousel-item v-for="item in carouselData" :key="item.id">    <img :src="item.url" class="carouselImg"/>    <span class="carouselSpan">{{ item.title }}</span>   </el-carousel-item>  </el-carousel>  </div> <footer1></footer1> <img src="../assets/a1.png" class="" width="100%"/> </div></template><script>  import footer1 from '../components/public/footer'  export default {  data: () => ({   carouselData:[   {url:require('../assets/a1.png'),title:'你看我叼嗎1',id:1},   {url:require('../assets/a3.png'),title:'你看我叼嗎2',id:2},   {url:require('../assets/a4.png'),title:'你看我叼嗎3',id:3}   ]  }),  components:{      footer1    }, }</script><style lang="scss">  @import '../style/mixin';  .carouselBlock{    width: 100%;    height: REM(300);    position:relative;    .carouselImg{    height: REM(300);    width:100%;   }   .carouselSpan{    position: absolute;    bottom: REM(20);    left: REM(20);    font-size: REM(24);    font-weight: bold;   }  }  .el-carousel__container{    width: 100%;    height: REM(300);  } .el-carousel__item h3 {  color: #475669;  font-size: 14px;  opacity: 0.75;  margin: 0; } .el-carousel__item:nth-child(2n) {   background-color: #99a9bf; } .el-carousel__item:nth-child(2n+1) {   background-color: #d3dce6; }</style>

PS:下面看下Vue.js中的圖片引用路徑

當我們在Vue.js項目中引用圖片時,關于圖片路徑有以下幾種情形:

使用一

我們在data里面定義好圖片路徑

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

然后,在template模板里面

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

這樣是錯誤的寫法,我們應該用v-bind綁定圖片的srcs屬性

:src="imgUrl">

或者

<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引入圖片路徑:

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

在data里面定義

avatar: avatar

情形三

我們也可以把圖片放在外層的static文件夾里面,然后在data里面定義:

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

以上就是我們在Vue.js中引用圖片路徑的方式。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 开封市| 万荣县| 建阳市| 盐亭县| 蓬安县| 合肥市| 皋兰县| 儋州市| 盐津县| 杭锦后旗| 临西县| 松溪县| 从化市| 琼海市| 南丹县| 二连浩特市| 噶尔县| 托克托县| 扬中市| 田东县| 仁布县| 汨罗市| 彭泽县| 锦州市| 丘北县| 峨山| 丽江市| 陆丰市| 萨嘎县| 锦屏县| 卢氏县| 芜湖市| 金昌市| 双辽市| 家居| 莲花县| 织金县| 类乌齐县| 陇西县| 玛纳斯县| 印江|