- 前端 (HTML + CSS + JavaScript/TypeScript + React/Vue/Angular)
这里以 React 为例,展示一个游戏列表组件。4er.cn
GameList.jsx (React)
jsx
import React, { useEffect, useState } from ‘react’;
function GameList() {
const [games, setGames] = useState([]);
useEffect(() => {
fetch('/api/games')
.then(response => response.json())
.then(data => setGames(data))
.catch(error => console.error('Error fetching games:', error));
}, []);
return (
<div>
<h1>游戏列表</h1>
<ul>
{games.map(game => (
<li key={game.id}>
{game.name} - 价格: {game.price}元
<button onClick={() => addToCart(gam