node.js - 链接到其他 Jade 文件

标签 node.js hyperlink express pug

我正在尝试了解 Express 和 Jade 的工作原理。

首先,当我使用 layout.jade 作为模板文件(页眉、正文、页脚)并使用不同的文件在正文中显示信息(参见下面的示例)时,我做得对吗?

代码运行良好,但我不确定这是否是在 Express 中做事的正确方法。如果我应该继续使用这种结构,我如何在内部从 index.jade 链接到其他文件(例如,About.jade),以显示该文件而不是 index.jade?

提前致谢!

layout.jade:

!!! 5
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
    script(type='text/javascript', src='https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js')
    script(type='text/javascript', src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js')
    script(type='text/javascript', src='/javascripts/external.js')

  // Header
  header#header

  // Navigation
  nav#nav
    // Navigation code (ul, li etc)...

  // Sidebar
  aside#sidebar
    // Sidebar code...

  // Body
  body!= body

index.jade:

!!! 5
html
  head
    title= title

    section#wrapper
      img.imageStyle(src = '/images/test1.png')
      // And so on...

关于.jade:

// You get it...

最佳答案

我认为您正在寻找的是快速查看渲染路线: http://expressjs.com/en/guide/using-template-engines.html

所以你可以这样设置:

app.get('/', function(req, res){
  res.render('index.jade', { title: 'index' });
});

app.get('/about', function(req, res){
  res.render('about.jade', { title: 'about' });
});

要从一个链接到另一个,一旦配置了正确的路由,您可以执行以下操作:

a(href='/') index

a(href='/about') about

更新另外,您不需要在索引中再次重复此操作。

!!! 5
html
  head
    title= title

关于node.js - 链接到其他 Jade 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10130215/

相关文章:

javascript - Node.js + MySQL - 处理事务

node.js - 运行 "ember server"失败并出现错误

ios - 从适用于 iOS 的 HTML5 应用程序调用号码的超链接

javascript - 如何将函数作为变量添加到链接 URL 中作为 Twitter 的推文?

node.js - 如何保护 express.js 中的路由?

node.js - 如何将 NextJS 应用程序部署到 Linux 服务器 (CentOS 7) - VPS

javascript - 如何在用户发送消息时停止 'is typing'功能

html - 如何在 css 中给 2 个不同的超链接不同的颜色

node.js - Node 快速路由发送文件失败取决于文件位置

node.js - 如何使用 Multer 从子对象上传文件