Vue實(shí)現(xiàn)讀取本地圖片的示例代碼
制作一個(gè)簡單的展示頁面
需要讀取本地靜態(tài)文件獲取文字信息 和確定圖片路勁
剛開始都是使用new XMLHttpRequest()。本地測(cè)試都沒有問題。但是部署到云服務(wù)器時(shí) 對(duì)圖片的判斷就失效了 無論請(qǐng)求地址多少 返回響應(yīng)碼都是200.(沒有找到原因)。 所以更換了方式 使用img的加載失敗回調(diào)方法。
url中文編碼問題(也是本地測(cè)試沒問題,線上就必須轉(zhuǎn)碼了)decodeURI()
// app.vue
<template>
<div id="app">
<div class="header">
<div class="main">
<div class="drug-name">
<span>{{drugName}}</span>
</div>
<div class="drug-img">
<img :src="drugImg" :alt="drugName" @error="imageLoadError">
</div>
<div class="drug-info" v-html="drugInfo"></div>
</div>
</div>
</div>
</template>
<script >
export default {
components: {
},
created() {
this.init()
this.OpenPup();
// this.imgChange();
console.log(this.$route.query)
},
data(){
return {
drugName: '',
drugInfo: '',
drugImg: '',
imgCheckCount: 0
}
},
methods:{
init(){
this.drugName=decodeURI(this.$route.query.drugName)
// this.drugName=decodeURI(this.$route.query.drugName)
console.log(this.$route.query.drugName)
// var destr = UrlDecode(str);
// this.imgChange()
this.drugImg = 'image/'+this.drugName+'.jpg'
},
imageLoadError(e){
console.log("onerror")
// this.drugImg = 'image/'+this.drugName+'.JPG'
// this.loadingOnOff = true
this.imgCheckCount += 1
this.imgCheks()
e.target.onerror = null
},
imgCheks(){
if(this.imgCheckCount === 1){
this.drugImg = 'image/'+this.drugName+'.JPG'
}else if(this.imgCheckCount === 2){
this.drugImg = 'image/'+this.drugName+'1.jpg'
}else if(this.imgCheckCount === 3){
this.drugImg = 'image/'+this.drugName+'1.JPG'
}
},
imgChange(){
// console.log("ceshi"+this.drugName)
// console.log("ceshi"+this.isFileExist('image/'+this.drugName+'.jpg'))
// console.log("ceshi"+this.isFileExist('image/'+this.drugName+'1.jpg'))
if(this.checkImg('image/'+this.drugName+'.jpg')){
this.drugImg = 'image/'+this.drugName+'.jpg'
}else if(this.checkImg('image/'+this.drugName+'.JPG')){
this.drugImg = 'image/'+this.drugName+'.JPG'
}else if(this.checkImg('image/'+this.drugName+'1.jpg')){
this.drugImg = 'image/'+this.drugName+'1.jpg'
}else if(this.checkImg('image/'+this.drugName+'1.JPG')){
this.drugImg = 'image/'+this.drugName+'1.JPG'
}
// console.log(this.drugImg)
// console.log(this.isFileExist('image/'+this.drugName+'.jpg'))
},
OpenPup(){
let xhr = new XMLHttpRequest()
// let okStatus = document.location.protocol === "file:" ? 0 : 200;
// xhr.open("GET", val.url, false); // 線上鏈接地址
// let drugUrl = this.drugName+".text"
let drugUrl = 'drug/'+this.drugName+".txt"
xhr.open("GET", drugUrl, false); // public文件夾下的絕對(duì)路徑
xhr.overrideMimeType("txt/html;charset=utf-8")
xhr.send(null)
// console.log(xhr.responseText) // xhr.responseText為文本中的內(nèi)容
this.drugInfo = xhr.responseText
// console.log(this.drugInfo)
},
// // 判斷本地是否有該文件
// showimg (url) {
// // 判斷結(jié)果
// let result = false
// url = './' + url
// // 獲取該目錄下所有文件名
// const files = require.context('public/image/', false, /\.jpg$/)
// console.log(files.keys())
// const filedata = files.keys()
// // 判斷是否有該文件
// filedata.forEach((item) => {
// if (item === url) {
// result = true
// }
// })
// // 返回結(jié)果
// return result
// },
isFileExist(filePath) {
let xhr = new XMLHttpRequest();
xhr.open('GET', filePath, false);
xhr.send();
console.log(xhr)
return xhr.status === 200;
},
checkImg(imgurl) {
var imgObj = new Image();
imgObj.src = imgurl;
imgObj.onload = function (res) {
return true;
console.log('有效鏈接')
}
imgObj.onerror = function (err) {
return false;
console.log('無效鏈接')
}
}
}
};
</script>
<style lang="less">
//#app {
// font-family: Avenir, Helvetica, Arial, sans-serif;
// -webkit-font-smoothing: antialiased;
// -moz-osx-font-smoothing: grayscale;
// text-align: center;
// color: #2c3e50;
//}
html,body{ width: 100%; height: 100%; overflow: scroll;}
html::-webkit-scrollbar, body::-webkit-scrollbar{width:0px;height:0px;}
body{
margin: 0;
padding: 0;
}
#app{
width: 100vw;
margin: 0;
padding: 0;
}
.header {
/*line-height: 1.5;*/
/*max-height: 100vh;*/
margin: 0;
padding: 0;
width: 100vw;
background-color: blanchedalmond;
}
.main{
margin: 0 5vw;
width: 90vw;
min-height: 100vh;
background-color: white;
}
.drug-name{
margin-bottom: 10vw;
padding-top: 10vw;
text-align:center
}
.drug-name span{
/*margin: auto;*/
/*margin-left: 50px;*/
color: #181818;
font-size: 32px;
font-weight: bold;
display: inline-block;
margin: 0 auto;
text-align: center;
}
.drug-img {
text-align:center
}
.drug-img img{
max-width: 80vw;
height: auto;
width: auto;
margin: auto;
text-align:center
}
.drug-info {
margin: 5vw 5vw 0;
padding-bottom: 5vw;
font-size: 16px;
color: #181818;
line-height: 30px;
overflow: auto;
}
.drug-info span{
font-size: 16px;
color: #181818;
}
h1 {
font-size: 20px;
text-align: left;
margin-bottom: 10px;
}
::v-deep p{
margin-bottom: 15px;
}
::v-deep b {
font-size: 19px;
font-weight: bold;
/*margin-bottom: 15px;*/
/*padding-bottom: 15px;*/
text-indent:2em;
}
</style>以上就是Vue實(shí)現(xiàn)讀取本地圖片的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Vue讀取本地圖片的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue3+Element-Plus實(shí)現(xiàn)左側(cè)菜單折疊與展開功能示例
本文主要介紹了Vue3+Element-Plus實(shí)現(xiàn)左側(cè)菜單折疊與展開功能示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
vue項(xiàng)目啟動(dòng)命令個(gè)人學(xué)習(xí)記錄
最近想要學(xué)習(xí)vue,正好看到資料,如何通過命令創(chuàng)建vue項(xiàng)目的方法,就留個(gè)筆記,下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目啟動(dòng)命令的相關(guān)資料,需要的朋友可以參考下2023-02-02
Vue 創(chuàng)建組件的兩種方法小結(jié)(必看)
Vue 創(chuàng)建組件的方法有哪些呢?下面小編就為大家分享一篇Vue 創(chuàng)建組件的兩種方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02
vue操作動(dòng)畫的記錄animate.css實(shí)例代碼
這篇文章主要介紹了vue操作動(dòng)畫的記錄animate.css的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
vue項(xiàng)目中chunk-vendors.js提示報(bào)錯(cuò)的查看方法(最新推薦)
在vue項(xiàng)目中,chunk-vendors.js報(bào)出的錯(cuò)誤提示經(jīng)常會(huì)導(dǎo)致開發(fā)者困惑,正確查看錯(cuò)誤的方法是從錯(cuò)誤提示的詳細(xì)信息中找到報(bào)錯(cuò)原因,下面給大家分享vue項(xiàng)目中chunk-vendors.js提示報(bào)錯(cuò)的查看方法,感興趣的朋友一起看看吧2024-12-12
從Vuex中取出數(shù)組賦值給新的數(shù)組,新數(shù)組push時(shí)報(bào)錯(cuò)的解決方法
今天小編就為大家分享一篇從Vuex中取出數(shù)組賦值給新的數(shù)組,新數(shù)組push時(shí)報(bào)錯(cuò)的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue中的eventBus會(huì)不會(huì)產(chǎn)生內(nèi)存泄漏你知道嗎
這篇文章主要為大家詳細(xì)介紹了vue中的eventBus會(huì)不會(huì)產(chǎn)生內(nèi)存泄漏,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02
關(guān)于vue中路由的跳轉(zhuǎn)和參數(shù)傳遞,參數(shù)獲取
這篇文章主要介紹了關(guān)于vue中路由的跳轉(zhuǎn)和參數(shù)傳遞,參數(shù)獲取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

