在Nextjs中使用getServerSideProps实现动态路由

发布时间:2022-05-08 / 作者:清心寡欲
本文介绍了在Nextjs中使用getServerSideProps实现动态路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力学习下一步。正在使用getServerSideProps计算路由。

使用免费API,我在DOM上显示了一个国家/地区列表。我希望动态链接到某个国家/地区,并获取和显示该特定国家/地区的数据。

以下是我到目前为止的代码

const Country = props => (
  
    

{props.country.name}

{props.country.capital}
); export async function getServerSideProps(context) { const { id } = context.query; const res = await fetch(`https://restcountries.eu/rest/v2/name/${id}`); const country = await res.json(); console.log(`Fetched place: ${country.name}`); return { props: { country } }; } export default Country;
  
Countries List

Countries{' '}

声明:本媒体部分图片、文章来源于网络,版权归原作者所有,如有侵权,请联系QQ:330946442删除。