页面无法显示图像?

huangapple go评论100阅读模式
英文:

The page cannot display images?

问题

前端代码已成功获取后端发送的图像链接,但无法显示它们。在检查控制台中的 "cards" 后,显示 imgUrl 已成功检索。当确保图像链接有效且没有热链接保护等问题时,仍无法显示。

以下是完整的代码:

<template>
  <div v-if="isLoading" class="loading-message">...</div>
  <div v-else class="card-container">
    <el-backtop :right="100" :bottom="100" />
    <div class="card" v-for="(card, index) in cards" :key="index">
      <div class="add-icon" @click="addToFavorites(card)">
        <el-icon><IEpCirclePlus /></el-icon>
      </div>
      <div class="card-content">
        <div class="left-content">
          <img
              v-if="card.imgUrl"
              :src="card.imgUrl"
              alt="Card Image"
              class="card-image"
              @click="handleButtonClick(card.link, index)"
              @mouseover="card.isRotated = true"
              @mouseout="card.isRotated = false"
              :style="{ transform: card.isRotated ? 'rotate(360deg)' : 'rotate(0deg)' }"
          />
          <div v-else class="loading-message">...</div>
        </div>
        <div class="right-content">
          <h3>{{ card.name }}</h3>
          <div class="divider"></div>
          <p>{{ card.description }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { ElMessageBox, ElMessage } from "element-plus";

export default {
  data() {
    return {
      cards: [],
      maxFavorites: 12,
      isLoading: true,
    };
  },
  methods: {
    async fetchDataFromApi() {
      try {
        const response = await axios.get("api/entertainmentTools/loadDataList");
        const responseData = response.data;
        if (responseData.status === "success" && responseData.code === 200) {
          this.cards = responseData.data.map((card) => ({ ...card, isRotated: false }));
          console.log(this.cards)
        } else {
          // Handle error
        }
      } catch (error) {
        // Handle error
      } finally {
        this.isLoading = false;
      }
    },
    handleButtonClick(link, index) {
      window.open(link, "_blank");
      this.cards.forEach((card, i) => {
        card.isRotated = index === i;
      });
    },
    addToFavorites(card) {
      const favorites = JSON.parse(localStorage.getItem("favorites") || "[]");
      if (favorites.length >= this.maxFavorites) {
        ElMessageBox.alert("12", "", {
          confirmButtonText: "",
          callback: () => {
            // Handle alert confirm
          },
        });
      } else {
        const favoriteCard = { name: card.name, imgUrl: card.imgUrl, link: card.link };
        favorites.push(favoriteCard);
        localStorage.setItem("favorites", JSON.stringify(favorites));
        ElMessage.success("Success");
      }
    },
  },
  async created() {
    await this.fetchDataFromApi();
  },
};
</script>

在代码的第一个版本中,考虑到在组件中进行数据检索和渲染过程中可能出现的潜在问题,代码已修改为其当前状态。但是,它仍然无法显示。

英文:

The frontend code has successfully obtained the image links sent by the backend, but it is unable to display them. After checking the console for "cards," it shows that the imgUrl has been successfully retrieved.When it is guaranteed, the image links are valid and free from issues such as hotlink protection.
enter image description here
Here is the complete code:

&lt;template&gt;
&lt;div v-if=&quot;isLoading&quot; class=&quot;loading-message&quot;&gt;...&lt;/div&gt;
&lt;div v-else class=&quot;card-container&quot;&gt;
&lt;el-backtop :right=&quot;100&quot; :bottom=&quot;100&quot; /&gt;
&lt;div class=&quot;card&quot; v-for=&quot;(card, index) in cards&quot; :key=&quot;index&quot;&gt;
&lt;div class=&quot;add-icon&quot; @click=&quot;addToFavorites(card)&quot;&gt;
&lt;el-icon&gt;&lt;IEpCirclePlus /&gt;&lt;/el-icon&gt;
&lt;/div&gt;
&lt;div class=&quot;card-content&quot;&gt;
&lt;div class=&quot;left-content&quot;&gt;
&lt;img
v-if=&quot;card.imgUrl&quot;
:src=&quot;card.imgUrl&quot;
alt=&quot;Card Image&quot;
class=&quot;card-image&quot;
@click=&quot;handleButtonClick(card.link, index)&quot;
@mouseover=&quot;card.isRotated = true&quot;
@mouseout=&quot;card.isRotated = false&quot;
:style=&quot;{ transform: card.isRotated ? &#39;rotate(360deg)&#39; : &#39;rotate(0deg)&#39; }&quot;
/&gt;
&lt;div v-else class=&quot;loading-message&quot;&gt;...&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;right-content&quot;&gt;
&lt;h3&gt;{{ card.name }}&lt;/h3&gt;
&lt;div class=&quot;divider&quot;&gt;&lt;/div&gt;
&lt;p&gt;{{ card.description }}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/template&gt;
&lt;script&gt;
import axios from &quot;axios&quot;;
import { ElMessageBox, ElMessage } from &quot;element-plus&quot;;
export default {
data() {
return {
cards: [],
maxFavorites: 12,
isLoading: true,
};
},
methods: {
async fetchDataFromApi() {
try {
const response = await axios.get(&quot;api/entertainmentTools/loadDataList&quot;);
const responseData = response.data;
if (responseData.status === &quot;success&quot; &amp;&amp; responseData.code === 200) {
this.cards = responseData.data.map((card) =&gt; ({ ...card, isRotated: false }));
console.log(this.cards)
} else {
// 
}
} catch (error) {
// 
} finally {
this.isLoading = false;
}
},
handleButtonClick(link, index) {
window.open(link, &quot;_blank&quot;);
this.cards.forEach((card, i) =&gt; {
card.isRotated = index === i;
});
},
addToFavorites(card) {
const favorites = JSON.parse(localStorage.getItem(&quot;favorites&quot;) || &quot;[]&quot;);
if (favorites.length &gt;= this.maxFavorites) {
ElMessageBox.alert(&quot;12&quot;, &quot;&quot;, {
confirmButtonText: &quot;&quot;,
callback: () =&gt; {
},
});
} else {
const favoriteCard = { name: card.name, imgUrl: card.imgUrl, link: card.link };
favorites.push(favoriteCard);
localStorage.setItem(&quot;favorites&quot;, JSON.stringify(favorites));
ElMessage.success(&quot;success&quot;);
}
},
},
async created() {
await this.fetchDataFromApi();
},
};
&lt;/script&gt;

In the first version of the code, considering the potential issues that might arise during the data retrieval and rendering process in the component, the code was modified to its current state. However, it still fails to display.

答案1

得分: 0

card.imgUrl与您的数据键imgurl不匹配。字母"u"没有大写。

英文:

card.imgUrl doesn't match your data key imgurl. the u is not capitalized.

huangapple
  • 本文由 发表于 2023年8月5日 15:48:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76840629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定