简体   繁体   English

react.js 不显示获取的图像

[英]react.js doesn't display fetched image

My component successfully fetches info (text and image) from Django backend but fails to display the image.我的组件成功地从 Django 后端获取信息(文本和图像),但无法显示图像。

when I checked the console and I saw the following error: Error while trying to use the following icon from the Manifest: http://localhost:3000/logo192.png (Download error or resource isn't a valid image)当我检查控制台并看到以下错误时:尝试使用清单中的以下图标时出错:http://localhost:3000/logo192.png(下载错误或资源不是有效图像)

I removed logo192.png & logo512.png from manifest.json and from HTML but still, the image doesn't display.我从 manifest.json 和 HTML 中删除了 logo192.png 和 logo512.png,但图像仍然没有显示。

React component;反应组件;

import React, { useState, useEffect, Fragment} from 'react';
import axios from 'axios';
import './Schools.css';

function Schools() {
    const [languageCenters, setLanguageCenters] = useState ([]);

    useEffect(() => {
        const config = {
            headers: {
                'Content-Type': 'application/json'
            }
        };

        const getLanguageCenters = async () => {
            try {
                const res = await axios.get(`${process.env.REACT_APP_API_URL}/api/partners/list/`, config);
                setLanguageCenters(res.data);
            }
            catch (err) {

            }
        };

        getLanguageCenters();
    }, []);

    const getAllLanguageCenters = () => {
        let allLanguageCenters = [];
        let results = [];

        languageCenters.map(languageCenter => {
            console.log(languageCenter.photo)
            return allLanguageCenters.push(
                <Fragment key={languageCenter.id}>
                    <div className='school__display'>
                        <img className='school__display__image' src={languageCenter.photo} alt='school logo' />
                        
                    </div>
                    <h3 className='school__language__center'>{languageCenter.name}</h3>
                    <p className='school__course'>{languageCenter.course}</p>
                    <p className='school__about'>{languageCenter.note}</p>
                </Fragment>
            );
        });

        for (let i = 0; i < languageCenters.length; i += 3) {
            results.push(
                <div key={i} className='school__card__row'>
                    <div className='school__card'>
                        {allLanguageCenters[i]}
                    </div>
                    <div className='school__card'>
                        {allLanguageCenters[i+1] ? allLanguageCenters[i+1] : null}
                    </div>
                    <div className='school__card'>
                        {allLanguageCenters[i+2] ? allLanguageCenters[i+2] : null}
                    </div>
                </div>
            );
        }

        return results;
    };  

  return (
    <div className='schools'>
        <section className='schools__language__centers'>
            <div className='schools__row'>
                <h2 className='schools__subheading'>Here are our schools</h2>
            </div>
            {getAllLanguageCenters()}
        </section>
    </div>
  )
}

export default Schools

console.log(languageCenter);控制台日志(语言中心);

{id: 2, name: 'UCI', email: '', photo: '/media/partners/2022/06/07/uk.png', phone: '', …}
course: "Foundation"
create_date: "2022-06-07T11:57:29+02:00"
deposit: "free"
email: ""
id: 2
language: "English"
name: "UCI"
note: ""
phone: ""
photo: "/media/partners/2022/06/07/uk.png"
[[Prototype]]: Object

Keep your logo912.png and logo512.png in public folder.将您的 logo912.png 和 logo512.png 保存在公用文件夹中。

May be you deleted that image from public/ folder OR you removed icons: [] from manifest.json in public/可能是您从public/文件夹中删除了该图像,或者您删除了图标:[] from manifest.json in public/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM