当前位置: 首页 > 工具软件 > koa-pug > 使用案例 >

koa2-pug路由添加备忘方便自查

陶乐生
2023-12-01

koa2-pug路由添加备忘方便自查

框架:koa2

模板引擎:pug

用koa2-generator 创建项目后,用pug做页面展现,配置路由,将页面简单串起来,可以互相跳转访问。

路由配置步骤:

1./views/文件夹下新建一个pug,比如test.pug

2./routes/ 文件夹下新建js文件,如:test.js

const router = require ( 'koa-router' )()

router . prefix ( '/test' )


module . exports = router

3.编辑/routes/下index.js文件(项目创建后默认生成),添加一段代码:

router . get ( '/test' , async ( ctx , next ) => {
await ctx . render ( 'test' , {
title: '测试页' ,
link: [
{ rel: 'icon' , type: 'image/x-icon' , href: '/favicon.ico' }
]
})
})

4.编辑app.js文件

1)在文件头部引入test.js路由:

const test = require ( './routes/test' )

2)正文中间使用路由

// routes
app
. use ( test . routes (), test . allowedMethods ())

5.在views/index.pug中添加一个test.pug的链接:

a ( href = "/test" ) 测试页面
6.运行项目,完成。
 类似资料: