使用vue本地靜態(tài)圖片路徑的注意事項(xiàng)有哪些-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)使用vue本地靜態(tài)圖片路徑的注意事項(xiàng)有哪些的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

10年積累的成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有湘東免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

vue的本地靜態(tài)圖片路徑

使用vue本地靜態(tài)圖片路徑的注意事項(xiàng)有哪些

這里寫圖片描述

需求:如何components里面的index.vue怎樣能把a(bǔ)ssets里面的圖片拿出來。

1.在img標(biāo)簽里面直接寫上路徑:

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

2.利用數(shù)組保存再循環(huán)輸出:

<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里面的完整代碼是這個(gè):

<template>
 <p>  <p 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>
  </p>
 <footer1></footer1>
 <img src="../assets/a1.png" class="" width="100%"/>
 </p>
</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-carouselcontainer{
    width: 100%;
    height: REM(300);
  }
 .el-carouselitem h4 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  margin: 0;
 }
 .el-carouselitem:nth-child(2n) {
   background-color: #99a9bf;
 }
 .el-carouselitem:nth-child(2n+1) {
   background-color: #d3dce6;
 }
</style>

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

當(dāng)我們?cè)赩ue.js項(xiàng)目中引用圖片時(shí),關(guān)于圖片路徑有以下幾種情形:

使用一

我們?cè)赿ata里面定義好圖片路徑

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

然后,在template模板里面

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

這樣是錯(cuò)誤的寫法,我們應(yīng)該用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打包出來。

使用二

當(dāng)我們需要在js代碼里面寫圖片路徑的時(shí)候,如果我們?cè)赿ata里面寫

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

此時(shí)webpack只會(huì)把它當(dāng)做字符串處理從而找不到圖片地址,此時(shí)我們可以使用import引入圖片路徑:

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

在data里面定義

avatar: avatar

情形三

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

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

感謝各位的閱讀!關(guān)于“使用vue本地靜態(tài)圖片路徑的注意事項(xiàng)有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

文章題目:使用vue本地靜態(tài)圖片路徑的注意事項(xiàng)有哪些-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://bm7419.com/article28/dgcdcp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、網(wǎng)站設(shè)計(jì)公司、Google、網(wǎng)頁(yè)設(shè)計(jì)公司ChatGPT、網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站