我尝试使用reactjs构建电子商务应用程序,我在前端遇到了一个问题,页面不在浏览器中显示。这个页面是管理页面,可以做CRUD的产品。
下面是Admin.jsx代码
import React, { useState } from 'react';
function Admin() {
const [products, setProducts] = useState([]);
const [newProduct, setNewProduct] = useState('');
const handleAddProduct = () => {
if (newProduct !== '') {
setProducts([...products, newProduct]);
setNewProduct('');
}
};
const handleDeleteProduct = (index) => {
const updatedProducts = [...products];
updatedProducts.splice(index, 1);
setProducts(updatedProducts);
};
return (
<div>
<h2>Admin Page</h2>
<h3>Add Product</h3>
<input
type="text"
value={newProduct}
onChange={(e) => setNewProduct(e.target.value)}
/>
<button onClick={handleAddProduct}>Add</button>
<h3>Product List</h3>
<ul>
{products.map((product, index) => (
<li key={index}>
{product}
<button onClick={() => handleDeleteProduct(index)}>Delete</button>
</li>
))}
</ul>
</div>
);
}
export default Admin;
这是主应用程序
import React from 'react';
import './App.css';
import {BrowserRouter, Routes, Route} from 'react-router-dom';
import Layout from './components/Layout';
import Home from './pages/Home';
import About from './pages/About';
import Contact from './pages/Contact';
import Wishlist from './components/Wishlist';
import Login from './pages/Login';
import Register from './pages/Register';
import SingleProduct from './pages/SingleProduct';
import Cart from './components/Cart';
import Admin from './pages/Admin';
function App() {
return (
<>
<BrowserRouter>
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<Home />} />
<Route path="about-us" element={<About />} />
<Route path="contact" element={<Contact />} />
<Route path="wishlist" element={<Wishlist />} />
<Route path="login" element={<Login />} />
<Route path="register" element={<Register />} />
<Route path="product/:id" element={<SingleProduct />} />
<Route path="cart" component={<Cart />} />
<Route path="admin" component={<Admin />} />
</Route>
</Routes>
</BrowserRouter>
</>
);
}
export default App;
文件位置已经正确,但仍然无法在浏览器上显示。任何帮助将不胜感激。谢谢你
2条答案
按热度按时间bmp9r5qi1#
我很确定这是因为你有
component={<Admin />}
,而它应该是element={<Admin />}
,就像所有其他的一样(除了cart
,我怀疑它也不起作用)ftf50wuq2#
从父布局布线中删除路径。更新这部分代码:-