加载了 moment.min.js 库后,我用脚本测试了一下,工作正常。 然后,我试着在html文档的正文中使用ejs引用相同的moment函数,我得到了 "moment is not defined "的错误信息。
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script>
const x = moment().format()
console.log(x)
</script>
然后,在正文中...
<body>
<h1><%= moment().format %></h1>
</body>
如果我不能解决这个问题,我将不得不找出一个变通的办法。 有什么办法?
在你的 EJS 文件中引用 moment。
var moment = require('moment');
app.locals.moment = require('moment');
在视图中
<h1><%= moment().format() %></h1>