Express,Jade和NodeJS:在页面之间导航

jan*_*niv 5 javascript node.js express pug

如何创建一个有两个按钮的Jade页面,其中每个按钮都重定向到另一个用Jade制作的页面?

Luc*_*ack 14

这是我为您的问题编写的代码:

server.js

var express = require('express');
var path = require('path');

var app = express(); 

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

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

app.get('/newpage', function(req, res){
  res.render('anotherpage', {
    title: 'Home'
  });
});
app.listen(3000);
Run Code Online (Sandbox Code Playgroud)

page1.jade

doctype html
html
  head
    title= title
  body
    p hi there!
    button(onclick="move()") newPage
script.
  function move() {
    window.location.href = '/newpage'
  }
Run Code Online (Sandbox Code Playgroud)

anotherpage.jade

doctype html
html
  head
    title= title
  body
    p welcome to the other page!
Run Code Online (Sandbox Code Playgroud)

享受,因为我花了15分钟写完所有这些和帖子.

卢卡

  • 这真的是最干净的方法吗?为什么不简单地使用 html 链接元素?而不是`button(onclick="move()") newPage` >> `a(href="/newpage")="New Page"`。像这样,不需要添加 `move()` 函数。 (2认同)