在 ReactJS 中匯入檔案和影像
在本文中,我們將瞭解如何將 CSS 檔案、影像和在其他資料夾中定義的函式匯入到主 App.js 檔案中。
在 React 中,我們需要從其資料夾中動態匯入影像。
示例
在這個示例中,我們將定義一個專案結構,其中影像和元件已在資產和元件資料夾中定義,然後我們將動態匯入它們到我們的主 App.js 檔案中。
專案結構
App.js
import React from 'react'; import MyComponent from './components/my -component.js'; import TutorialsPoint from './assets/img.png'; const App = () => { return ( <div> <img src={TutorialsPoint} /> <MyComponent /> </div> ); }; export default App;
my-component.js
import React from 'react'; const MyComponent = () => { return <div>Hii! This is the custom MyComponent</div>; }; export default MyComponent;
輸出
這將產生以下結果。
廣告