ale*_*xtc 5 javascript node.js express pug
在我的 Express JS Web 应用程序中,login
路由将一些变量呈现给登录 pug 视图。
在 login.js
router.get('/login', function(req, res, next) {
var locations = ["Location 1", "Location 2"];
var count = 0;
var title = 'Login';
console.log("req.originalUrl=" + req.originalUrl);
res.render('login', {
title: title, // Give a title to our page
jsonData: locations, // Pass data to the View
count: locations.length,
originalUrl: req.originalUrl
});
});
Run Code Online (Sandbox Code Playgroud)
在 login.pug
extends layout
block content
div(class='container mt-3')
h2 Welcome to #{title}, #{count}, #{originalUrl}
a(class="btn btn-primary" href="/location/new" role="button") NEW
br
br
ul#locList(class='list-group')
for location in jsonData
li(class="list-group-item")
a(href='#{originalUrl}' + '?' + location, class='list-group-item list-group-item-action')
h2=location
Run Code Online (Sandbox Code Playgroud)
中的originalUrl
变量a href
未被评估为“ http://localhost:3000/login?Location%201
”,而是“ http://localhost:3000/login#{originalUrl}?Location%201
”。
然后我不得不将其更改为 ' a(href=originalUrl + '?' + location, class='list-group-item list-group-item-action')
' 以使其工作。
简而言之,a(href='#{originalUrl}')
在工作时不起作用a(href=originalUrl)
,因为a href
.
但是,同一变量在“ h2 Welcome to #{title}, #{count}, #{originalUrl}
”行被正确评估为“ Welcome to Login, 2, /login
”。
如何在相同的变量进行评估不同的a href
距离h2
?
Mar*_*yer 19
这是几个版本前出现的已知行为(我认为是 2016 年)。#{style}
属性中不支持此插值:
警告
以前版本的 Pug/Jade 支持插值语法,例如:
a(href="/#{url}") Link 此语法不再受支持。替代方法见下文。(查看我们的迁移指南,了解有关 Pug v2 和以前版本之间其他不兼容性的更多信息。)
更多信息请参见:https : //pugjs.org/language/attributes.html
您应该能够使用常规模板文字:
a(href=`${originalUrl}`)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13352 次 |
最近记录: |