vue中本地靜態(tài)圖片路徑寫法
這里寫圖片描述
需求:如何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里面的完整代碼是這個:
<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中的圖片引用路徑
當(dāng)我們在Vue.js項(xiàng)目中引用圖片時,關(guān)于圖片路徑有以下幾種情形:
使用一
我們在data里面定義好圖片路徑
imgUrl:'../assets/logo.png'
然后,在template模板里面
<<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">img src="
{{imgUrl}}">
這樣是錯誤的寫法,我們應(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代碼里面寫圖片路徑的時候,如果我們在data里面寫
imgUrl:'../assets/logo.png'
此時webpack只會把它當(dāng)做字符串處理從而找不到圖片地址,此時我們可以使用import引入圖片路徑:
:src="avatar" /> import avatar from '@/assets/logo.png'
在data里面定義
avatar: avatar
情形三
我們也可以把圖片放在外層的static文件夾里面,然后在data里面定義:
imgUrl : '../../static/logo.png' :src="imgUrl" />
以上就是我們在Vue.js中引用圖片路徑的方式。
相關(guān)文章
vue中使用echarts并根據(jù)選擇條件動態(tài)展示echarts圖表
雖然老早就看過很多echarts的例子, 但自己接觸的項(xiàng)目中一直都沒有真正用到過,直到最近才開始真正使用,下面這篇文章主要給大家介紹了關(guān)于vue中使用echarts并根據(jù)選擇條件動態(tài)展示echarts圖表的相關(guān)資料,需要的朋友可以參考下2023-12-12
vue+element+springboot實(shí)現(xiàn)文件下載進(jìn)度條展現(xiàn)功能示例
本文主要介紹了vue + element-ui + springboot 實(shí)現(xiàn)文件下載進(jìn)度條展現(xiàn)功能,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
vue+element-ui中form輸入框無法輸入問題的解決方法
很多初次接觸element-ui的同學(xué),在用到element form組件時可能會遇到input框無法輸入文字的問題,下面這篇文章主要給大家介紹了關(guān)于vue+element-ui中form輸入框無法輸入問題的解決方法,需要的朋友可以參考下2023-04-04
vue在手機(jī)中通過本機(jī)IP地址訪問webApp的方法
這篇文章主要介紹了vue在手機(jī)中通過本機(jī)IP地址訪問webApp的方法,需要的朋友可以參考下2018-08-08
淺談Vue render函數(shù)在ElementUi中的應(yīng)用
今天小編就為大家分享一篇淺談Vue render函數(shù)在ElementUi中的應(yīng)用,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue項(xiàng)目中v-model和sync的區(qū)別及使用場景分析
在Vue項(xiàng)目中,v-model和.sync是實(shí)現(xiàn)父子組件雙向綁定的兩種方式,v-model主要用于表單元素和子組件的雙向綁定,通過modelValue和update:modelValue實(shí)現(xiàn),.sync修飾符則用于同步prop值,適合在子組件內(nèi)更新父組件prop值的場景,通過update:propName事件實(shí)現(xiàn)2024-11-11
解決vue項(xiàng)目運(yùn)行npm run serve報錯的問題
這篇文章主要介紹了解決vue項(xiàng)目運(yùn)行npm run serve報錯的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10

