【问题标题】:need to access location.hash in jade, wintersmith需要在玉中访问 location.hash,wintersmith
【发布时间】:2019-06-01 20:44:52
【问题描述】:

我正在使用 Wintersmith,需要将 location.hash 作为变量访问。

这是我尝试过的,但它产生了Cannot read property 'location' of undefined 错误:

- var hash = window.location.hash
- var articles =  env.helpers.getArticles(contents, hash)
img(data-item = i, src="img/events/"+hash+'/' +a)

【问题讨论】:

  • 错误信息是什么?
  • @AngelM。无法读取未定义的属性“位置”

标签: pug wintersmith


【解决方案1】:

您将 pug/jade 代码与浏览器 JavaScript 代码混合在一起。 Jade/pug 在服务器上运行,因此没有可用的“窗口”变量:

window.location.hash

您需要在 res.render 语句中将其作为变量从您的路由中传入:

res.render('<<viewName>>', { hash: req.query });

先看看快递req.query docs。

【讨论】:

  • 怎么样?所以,我也无法访问 paginator.coffee。
  • 如果您对 paginator.coffee 也有疑问,您可能需要编辑您的问题(我不确定那是什么)
  • 哦,,,我没有使用 Express.. 它在 Wintersmith。
  • 那么你需要进入 Wintersmith 文档,看看如何在调用 pug 模板的任何地方访问请求查询参数。
猜你喜欢
  • 2013-03-05
  • 2013-12-10
  • 2017-07-25
  • 1970-01-01
  • 2020-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多