教程 > next.js 中文教程 > 阅读:755

next.js 命令式(imperative)路由——迹忆客-ag捕鱼王app官网

在 next.js 中,到目前为止,我们使用 link react 组件从一个页面导航到另一个页面。 还有一种编程方式可以使用路由器组件实现相同的目标。 通常路由器组件与 html 标签一起使用。

更新 pages 目录中的 index.js 文件,如下所示。

index.js

import router from 'next/router'
import head from 'next/head'
function homepage(props) {
   return (
      <>
         
            welcome to 迹忆客 - next.js 教程!
         
         
welcome to 迹忆客 - next.js 教程!
router.push('/posts/one')}>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

启动 next.js 服务器

运行以下命令启动服务器

$ 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 453 ms (124 modules)

在浏览器中打开 localhost:3000 ,我们将看到以下输出。

nextjs imperative 路由

单击 post one,,它不是一个链接,但是可单击的。

nextjs imperative 路由 单击

查看笔记

扫码一下
查看教程更方便
网站地图