Node.js 报错 "title is not defined"
发布于 5 年前 作者 ChoCheckTsui 14054 次浏览 最后一次编辑是 3 年前

错误如下: ReferenceError: /Users/victor/mymicroblog/views/layout.ejs:4 2| <html> 3| <head>

4| <title><%= title %></title> 5| <link rel=‘stylesheet’ href=’/stylesheets/bootstrap.css’ /> 6| <style type=“text/css”> 7| body { title is not defined

访问页面locathost:3000/显示内容如下: ReferenceError: /Users/victor/mymicroblog/views/layout.ejs:4 2| <html> 3| <head> >> 4| <title><%= title %></title> 5| <link rel=‘stylesheet’ href=’/stylesheets/bootstrap.css’ /> 6| <style type=“text/css”> 7| body { title is not defined at eval (eval at <anonymous> (/Users/victor/mymicroblog/node_modules/ejs/lib/ejs.js:237:14), <anonymous>:29:89) at eval (eval at <anonymous> (/Users/victor/mymicroblog/node_modules/ejs/lib/ejs.js:237:14), <anonymous>:29:2010) at /Users/victor/mymicroblog/node_modules/ejs/lib/ejs.js:250:15 at Object.exports.render (/Users/victor/mymicroblog/node_modules/ejs/lib/ejs.js:288:13) at View.exports.renderFile [as engine] (/Users/victor/mymicroblog/node_modules/ejs/lib/ejs.js:318:20) at View.render (/Users/victor/mymicroblog/node_modules/express/lib/view.js:76:8) at Function.app.render (/Users/victor/mymicroblog/node_modules/express/lib/application.js:503:10) at ServerResponse.res.render [as partial] (/Users/victor/mymicroblog/node_modules/express/lib/response.js:802:7) at /Users/victor/mymicroblog/node_modules/express-partials/index.js:75:13 at View.exports.renderFile [as engine] (/Users/victor/mymicroblog/node_modules/ejs/lib/ejs.js:318:3)

我的layout.ejs相关代码片段如下: <!DOCTYPE html> <html> <head> <title><%= title %></title> <link rel=‘stylesheet’ href=’/stylesheets/bootstrap.css’ /> <style type=“text/css”> body { padding-top: 60px; padding-bottom: 40px;} </style> …

我的routes/index.js相关代码片段如下: router.get(’/’, function(req, res) { res.render(‘index’, { title: ‘首页’ }); });

7 回复

layout 里用locals.title

@nihgwu 十分感谢!按照你说的成功了!但请问为什么这么改就ok了呢?

@ChoCheckTsui 我也成功了,请问这是什么原理呢

@nihgwu 请问是什么原理啊 我也成功了

真能挖坟。。

原因就是 index 可以直接用 <%= title %> ,layout 里要用 <%= locals.title %>

回到顶部