扫码一下
查看教程更方便
next.js 使用基于文件系统的路由器。 每当我们将任何页面添加到 pages 目录时,它都会自动通过 url 获得。 以下是该路由器的规则。
next.js 允许使用 link react 组件在客户端链接页面。 它具有以下属性 -
让我们看一个示例。在此示例中,我们将更新 index.js 和 first.js 页面从而使服务器命中来获取数据。
让我们更新 全局 css 支持章节中使用的 nextjs 项目。
更新 pages 目录中的 index.js 文件,如下所示。
index.js
import link from 'next/link' import head from 'next/head' function homepage(props) { return ( <>
welcome to 迹忆客 - next.js 教程! welcome to 迹忆客 - next.js 教程!first post
next stars: {props.stars}) } export async function getserversideprops(context) { const res = await fetch('https://api.github.com/repos/vercel/next.js') const json = await res.json() return { props: { stars: json.stargazers_count } } } export default homepage
运行以下命令启动服务器
$ npm run dev
> nextjs@1.0.0 dev //workspace/node/nextjs
> next dev
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
event - compiled client and server successfully in 418 ms (124 modules)
在浏览器中打开 localhost:3000 ,我们将看到以下输出。
点击first post 链接。