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

next.js 静态文件服务——迹忆客-ag捕鱼王app官网

next.js 支持将静态文件(例如图片)存放到根目录下的 public 目录中,并对外提供访问。public 目录下存放的静态文件的对外访问路径以 (/) 作为起始路径。

在 next.js 中,页面是一个 react 组件,从 pages 目录中导出。 每个页面都根据其文件名与一个路由相关联。

让我们更新 pages 章节中使用的 nextjs 项目。

创建公共目录并在其中放置任何图像。 我们使用用 logo.png,迹忆客的图标。

更新 first.js 如下

first.js

import link from 'next/link'
export default function firstpost() {
   return (
      <>
         
         
         
) }

在这里,我们在 index.js 文件中添加了对 logo.png 的引用。


启动 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 379 ms (110 modules)
wait  - compiling / (client and server)...
event - compiled client and server successfully in 181 ms (146 modules)
wait  - compiling /_error (client and server)...
event - compiled client and server successfully in 92 ms (147 modules)
wait  - compiling /posts/first...
event - compiled client and server successfully in 246 ms (149 modules)
wait  - compiling...
event - compiled successfully in 54 ms (101 modules)

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

nextjs 静态文件服务

查看笔记

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