欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue實現(xiàn)讀取本地圖片的示例代碼

 更新時間:2023年07月16日 15:52:28   作者:小和尚敲木頭  
這篇文章主要為大家詳細介紹了如何利用Vue實現(xiàn)讀取本地圖片的功能,文中的示例代碼講解詳細,具有一定的參考價值,需要的小伙伴可以學習一下

制作一個簡單的展示頁面

需要讀取本地靜態(tài)文件獲取文字信息 和確定圖片路勁

剛開始都是使用new XMLHttpRequest()。本地測試都沒有問題。但是部署到云服務器時 對圖片的判斷就失效了 無論請求地址多少 返回響應碼都是200.(沒有找到原因)。 所以更換了方式 使用img的加載失敗回調方法。

url中文編碼問題(也是本地測試沒問題,線上就必須轉碼了)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文件夾下的絕對路徑
      xhr.overrideMimeType("txt/html;charset=utf-8")
      xhr.send(null)
      // console.log(xhr.responseText)  // xhr.responseText為文本中的內容
      this.drugInfo = xhr.responseText
      // console.log(this.drugInfo)
    },
    // // 判斷本地是否有該文件
    // showimg (url) {
    //   // 判斷結果
    //   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
    //     }
    //   })
    //   // 返回結果
    //   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實現(xiàn)讀取本地圖片的示例代碼的詳細內容,更多關于Vue讀取本地圖片的資料請關注腳本之家其它相關文章!

相關文章

  • 解決vue?change阻止默認事件問題

    解決vue?change阻止默認事件問題

    這篇文章主要介紹了vue?change阻止默認事件問題,使用事件 @click.stop.native.prevent 解決 (使用@click.stop 或者 @click.prevent都無效,直接報錯還阻止不了事件),需要的朋友可以參考下
    2022-01-01
  • Vue3+Element-Plus實現(xiàn)左側菜單折疊與展開功能示例

    Vue3+Element-Plus實現(xiàn)左側菜單折疊與展開功能示例

    本文主要介紹了Vue3+Element-Plus實現(xiàn)左側菜單折疊與展開功能示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Avue?組件庫的使用初體驗

    Avue?組件庫的使用初體驗

    這篇文章主要為大家介紹了Avue?組件庫的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • vue項目啟動命令個人學習記錄

    vue項目啟動命令個人學習記錄

    最近想要學習vue,正好看到資料,如何通過命令創(chuàng)建vue項目的方法,就留個筆記,下面這篇文章主要給大家介紹了關于vue項目啟動命令的相關資料,需要的朋友可以參考下
    2023-02-02
  • Vue 創(chuàng)建組件的兩種方法小結(必看)

    Vue 創(chuàng)建組件的兩種方法小結(必看)

    Vue 創(chuàng)建組件的方法有哪些呢?下面小編就為大家分享一篇Vue 創(chuàng)建組件的兩種方法小結,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • vue操作動畫的記錄animate.css實例代碼

    vue操作動畫的記錄animate.css實例代碼

    這篇文章主要介紹了vue操作動畫的記錄animate.css的相關知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-04-04
  • vue項目中chunk-vendors.js提示報錯的查看方法(最新推薦)

    vue項目中chunk-vendors.js提示報錯的查看方法(最新推薦)

    在vue項目中,chunk-vendors.js報出的錯誤提示經常會導致開發(fā)者困惑,正確查看錯誤的方法是從錯誤提示的詳細信息中找到報錯原因,下面給大家分享vue項目中chunk-vendors.js提示報錯的查看方法,感興趣的朋友一起看看吧
    2024-12-12
  • 從Vuex中取出數(shù)組賦值給新的數(shù)組,新數(shù)組push時報錯的解決方法

    從Vuex中取出數(shù)組賦值給新的數(shù)組,新數(shù)組push時報錯的解決方法

    今天小編就為大家分享一篇從Vuex中取出數(shù)組賦值給新的數(shù)組,新數(shù)組push時報錯的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • vue中的eventBus會不會產生內存泄漏你知道嗎

    vue中的eventBus會不會產生內存泄漏你知道嗎

    這篇文章主要為大家詳細介紹了vue中的eventBus會不會產生內存泄漏,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • 關于vue中路由的跳轉和參數(shù)傳遞,參數(shù)獲取

    關于vue中路由的跳轉和參數(shù)傳遞,參數(shù)獲取

    這篇文章主要介紹了關于vue中路由的跳轉和參數(shù)傳遞,參數(shù)獲取方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評論