0

I am using Wintersmith and need to access the location.hash as a variable.

This is what I tried, but it is producing a Cannot read property 'location' of undefined error:

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

1 回答 1

0

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

window.location.hash

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

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

先看一下 Express req.query 文档

于 2019-01-07T14:01:27.817 回答