矩没有被定义--试图用ejs引用矩函数。

问题描述 投票:0回答:1

加载了 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>

如果我不能解决这个问题,我将不得不找出一个变通的办法。 有什么办法?

javascript momentjs ejs
1个回答
0
投票

在你的 EJS 文件中引用 moment。

var moment = require('moment');
app.locals.moment = require('moment');

在视图中

<h1><%= moment().format() %></h1> 
© www.soinside.com 2019 - 2024. All rights reserved.