0

当我通过硬编码的 teamName 时,会显示正确的结果,但想使用 pathParam 从 url 传递 teamName

代码:

export const TeamPage = () => {

    const [team, setTeam] = useState({matches :[]});

    
    const {teamName} = useParams();

    useEffect(
        ()=>{
            const fetchMatches = async() =>{
               
                const response = await fetch('http://localhost:8787/team/${teamName}');
                const data = await response.json();
                setTeam(data);
            };
            fetchMatches();
        },[]

    );
    console.log(teamName);
    if(!team || !team.teamName){
        return <h1>Team not found</h1>
    }

    return (
        <div className="TeamPage">
            <h1>{team.teamName}</h1>
        </div>
    );
}
4

1 回答 1

1

我不确定,但我认为您使用了错误的语法,应该是:

const response = await fetch(`http://localhost:8787/team/${teamName}`);

所以 `` 而不是 ''

于 2021-07-01T14:24:08.567 回答