NodeJS怎么共享路由里面的数据呢?


比如说,我有四个视图文件 layout.jade / index.jade / archive.jade / header.jade

header.jade


 header.site-header
   nav.site-nav
     ul
       each li in siteNav
         li.nav-item
           a(href='#{li.url}', title='#{li.name}')= li.name

layout.jade


 doctype html
 html
   head
     title= title
     meta(charset='utf-8')
     link(rel='stylesheet', href='/stylesheets/font-awesome.css')
     link(rel='stylesheet', href='/stylesheets/style.css')
   body
     include ./header.jade
     main.site-main
       block content

index.jade archive.jade 都是直接 extends layout.jade

index.js


 var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res) {
  res.render('index', {
    siteNav: [
      {
      'name':'Home',
      'url': '/'
      },
      {
        'name': 'Archives',
        'url': '/archives'
      },
      {
        'name': 'About',
        'url': '/about'
      },
      {
        'name':  'Sign up',
        'url': '/join'
      },
      {
        'name': 'Sign in',
        'url': '/login'
      }
    ]
  });
});

/* get login page */
router.get('/login', function(req, res) {
  res.render('login',{});
});

我想在 login.jade 也要有菜单,怎么把 index 里面的共享给 login 呢?

Express node.js jade

Mr.Neko 10 years, 3 months ago

Your Answer