react 圖像識別_無法在React中基于URL查找圖像

react 圖像識別

If you're new to React and are having trouble accessing images stored locally, you're not alone.

如果您不熟悉React,并且無法訪問本地存儲的圖像,那么您并不孤單。

Imagine you have your images stored in a directory next to a component like this:

想象一下,您的圖像存儲在這樣的組件旁邊的目錄中:

/src/components- component1- component2
/img- img1- img2

And you're trying to access the images in the /img directory from component2:

而且您正在嘗試從component2訪問/img目錄中的圖像:

import React, { Component, useState, useEffect } from 'react';
import { render } from 'react-dom'
import { useTransition, animated, config } from "react-spring";
import imgArr from './images';
import '../App.css';const Slideshow = () => {const [index, set] = useState(0)const transitions = useTransition(imgArr[index], item => item.id, {from: { opacity: 0 },enter: {opacity: 1 },leave: { opacity: 0 },config: config.molasses,})useEffect(() => void setInterval(() => set(state => (state + 1) % 4), 2000), [])return transitions.map(({ item, props, key }) => (<animated.divkey={key}className="bg"style={{ ...props, slideshowContainerStyle}}><img className="img" src={require(item.url)} alt=""/></animated.div>))
}const slideshowContainerStyle = {width: '80%',height: '300px'
}export default Slideshow;

You've tried the paths ../img/img1.jpg and ..img/img1.jpg, but get Error: Cannot find module '<path>' .

您已經嘗試了../img/img1.jpg..img/img1.jpg路徑,但得到Error: Cannot find module '<path>'

So what's going on here?

那么這是怎么回事?

關于create-react-app (A little about create-react-app)

Like most people, you probably used create-react-app to bootstrap your project.

像大多數人一樣,您可能使用了create-react-app來引導您的項目。

In that case, using relative paths can be a bit tricky because create-react-app builds the HTML, CSS, and JavaScript files to an output folder:

在這種情況下,使用相對路徑可能會有些棘手,因為create-react-app將HTML,CSS和JavaScript文件構建到輸出文件夾中:

/public- index.html- bundle.js- style.css

There are a number of ways to use images with create-react-app, but one of the easiest is to include your images into /public. When your project is built, /public serves as the root directory.

通過create-react-app有多種使用圖像的方法,但是最簡單的方法之一就是將圖像包含在/public 。 構建項目時, /public充當根目錄。

You can read more about adding images or other assets in the Create React App docs.

您可以在Create React App文檔中閱讀有關添加圖像或其他資產的更多信息。

匯入圖片 (Importing images)

If you took a look at the docs, you'll notice that the code includes import statements to load assets like images and fonts.

如果您看一下文檔,您會注意到該代碼包含import語句,以加載諸如圖像和字體之類的資產。

Importing is useful when your asset, in this case an image, is in the same directory or near the component that uses it, and won't be used anywhere else. For example, if you have an input component with buttons that use SVGs for thumbs up and thumbs down icons.

當您的資產(在這種情況下為映像)位于使用該資產的目錄中或該組件附近,并且在其他任何地方都不會使用時,導入很有用。 例如,如果您有一個帶有按鈕的輸入組件,這些按鈕使用SVG表示大拇指和大拇指向下的圖標。

A bit advantage of using import is that it will throw an error during build time if there's a typo. This sort of checking helps ensure users won't see a broken image that slipped by.

使用import一個優點是,如果有錯字,它將在構建期間引發錯誤。 這種檢查有助于確保用戶不會看到滑動的破損圖像。

翻譯自: https://www.freecodecamp.org/news/unable-to-find-images-based-on-url-in-react/

react 圖像識別

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390176.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390176.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390176.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

html單行元素居中顯示,多行元素居左顯示

有很多的業務需要元素或者文字如果單行&#xff0c;居中顯示&#xff0c;如果數據增多&#xff0c;居中顯示代碼&#xff08;直接復制到編輯器可用&#xff09;&#xff1a;<!DOCTYPE html> <html lang"en"> <head> <meta charset"UTF-8&q…

ML.NET 0.2版增加了集群和新示例

在今年的Build大會上&#xff0c;微軟首次發布了ML.NET。ML.NET是開源的、跨平臺的以及運行在.NET上的機器學習框架。微軟的Ankit Asthana宣布該項目已經完成了第二版的開發。第二版增加了幾個新功能&#xff0c;包括名為集群的新機器學習任務&#xff0c;交叉驗證和訓練-測試&…

如何變得井井有條-來之不易的秘訣來組織您的生活

Because of the changes brought about by COVID-19, many people have had to find healthy and productive ways of working remotely. 由于COVID-19帶來的變化&#xff0c;許多人不得不尋找健康有效的遠程工作方式。 Some have been sent home and can continue doing thei…

被未知進程占用端口的解決辦法

echo off echo 這是用來結束一個未知進程占用端口的批處理可執行文件ipconfig /allnetstat -anoecho 請查看以上信息&#xff0c;輸入被占用的端口號:set /p port請輸入port:tasklist|findstr %port%echo 請結合上述程序進行輸入&#xff0c;請**謹慎輸入**set /p program請輸入…

怎樣在減少數據中心成本的同時不犧牲性能?

2019獨角獸企業重金招聘Python工程師標準>>> 導讀雖然組織對數據中心提出了更高的要求&#xff0c;但IT管理人員確實有辦法在嚴格的預算內展開工作。如今&#xff0c;組織認為即使性能預期不斷提高&#xff0c;其數據中心預算也在縮減。盡管2018年IT支出總體預計增長…

賽普拉斯 12864_如何使用賽普拉斯自動化輔助功能測試

賽普拉斯 12864In my previous post, I covered how to add screenshot testing in Cypress to ensure components dont unintentionally change over time. 在上一篇文章中 &#xff0c;我介紹了如何在賽普拉斯中添加屏幕截圖測試&#xff0c;以確保組件不會隨時間變化。 Now…

anaconda在win下和在mac下的安裝區別

1. 在win下安裝anaconda后會提示你選擇環境變量&#xff0c;但是建議使用默認。 于是CMD進入終端和使用navigator進入終端不一樣&#xff0c;前者會提示無此命令&#xff0c;只能通過navigator進入終端 即使在系統變量變量Path里添加了路徑&#xff0c;使用CMD還是不能使用pyth…

fcn從頭開始_如何使用Go從頭開始構建區塊鏈

fcn從頭開始介紹 (Introduction) With Web 3.0 and blockchain becoming more mainstream every day, do you know what blockchain is? Do you know its technical advantages and use-cases?隨著Web 3.0和區塊鏈每天變得越來越主流&#xff0c;您知道什么是區塊鏈嗎&#x…

java實現無序數組結構

一、數組的2種定義方式 數據類型 [] 數組名稱 new 數據類型[數組長度]; 這里 [] 可以放在數組名稱的前面&#xff0c;也可以放在數組名稱的后面&#xff0c;一般放在名稱的前面 數據類型 [] 數組名稱 {數組元素1&#xff0c;數組元素2&#xff0c;......} 這種方式聲明數組的…

Android App 的主角:Activity

Android App 程序主要由4種類型組成&#xff1a; 1.Activity&#xff08;活動&#xff09;&#xff1a;主要負責屏幕顯示畫面&#xff0c;并處理與用戶的互動。每個Android App至少都會有一個Activity&#xff0c;在程序一啟動時顯示主畫面供用戶操作。 2.Service&#xff08;后…

通過構建Paint App學習React Hooks

According to people in the know, React Hooks are hot, hot, hot. In this article, we follow Christian Jensens 14-part tutorial to find out about the basics of this new feature of React. Follow along to find out more! 據知情人士稱&#xff0c;React Hooks很熱&…

正則表達式 匹配常用手機號 (13、15\17\18開頭的十一位手機號)

原文:正則表達式 匹配常用手機號 &#xff08;13、15\17\18開頭的十一位手機號&#xff09;^1[3578]\d{9}$ ^1表示以1開頭&#xff0c;[3578]表示第二位的數字為3578中的任意一個&#xff0c;\d{9}表示0~9范圍內的數字匹配九次,$表示結束&#xff0c;12位以上的數字不匹配。

Npoi導出excel整理(附源碼)

前些日子做了一個簡單的winform程序&#xff0c;需要導出的功能&#xff0c;剛開始省事直接使用微軟的組件&#xff0c;但是導出之后發現效率極其低下&#xff0c;絕對像web那樣使用npoi組件&#xff0c;因此簡單的進行了整理&#xff0c;包括直接根據DataTable導出excel及Data…

44. 通配符匹配

44. 通配符匹配 給定一個字符串 (s) 和一個字符模式 &#xff0c;實現一個支持 ‘?’ 和 ‘*’ 的通配符匹配。 ? 可以匹配任何單個字符。 * 可以匹配任意字符串&#xff08;包括空字符串&#xff09;。 兩個字符串完全匹配才算匹配成功。說明: s 可能為空&#xff0c;且…

遞歸javascript_使用freeCodeCamp挑戰解釋了JavaScript中的遞歸

遞歸javascriptIn this article I will touch on a few important ideas to help you understand Recursion in JavaScript. I’m not going to give a full definition here, but you can take a look at what Wikipedia has to say. 在本文中&#xff0c;我將介紹一些重要的想…

入庫成本與目標成本對比報表中我學到的東西

1、SQL方面&#xff1a; &#xff08;1&#xff09;、用DECODE函數解決除數為零的情況 具體語法&#xff1a; DECODE&#xff08;參數&#xff0c;0&#xff0c;1&#xff0c;參數&#xff09; ->DECODE(TAB1.A8&#xff0c;0&#xff0c;1&#xff0c;TAB1.A8) &#xff08…

J - Borg Maze

J - Borg Maze 思路&#xff1a;bfs最小生成樹。#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 110 using namespace std; int fa[MAXN]; struct nond{int x,y,z; }v[MAXN*MAXN]; s…

1095. 山脈數組中查找目標值

1095. 山脈數組中查找目標值 &#xff08;這是一個 交互式問題 &#xff09; 給你一個 山脈數組 mountainArr&#xff0c;請你返回能夠使得 mountainArr.get(index) 等于 target 最小 的下標 index 值。 如果不存在這樣的下標 index&#xff0c;就請返回 -1。 何為山脈數組…

【小摘抄】關于C++11下 string各類用法(持續更新)

http://blog.csdn.net/autocyz/article/details/42391155 提供了最簡單的詳解 下列對本人近期開發中的一些心得體會進行摘抄 1.string按照字符進行截取 示例代碼&#xff1a; string teststring "#12313#kajlkfdsa";//通訊消息示例&#xff0c;結合string的內置函數…

sql綜合練習題

一、表關系 年級表&#xff1a;class_grade create table class_grade(gid int primary key auto_increment,gname varchar(20) not null); insert into class_grade(gname) values(一年級),(二年級),(三年級); 班級表&#xff1a;class create table class(cid int primary ke…