首页 > 解决方案 > 无法在 ejs 模板中调用函数

问题描述

我在调用 ejs 文件中的函数时遇到问题。

我有单独的 js 文件'main.js',它是

// this is main.js
var hello = function() {
   return "hello";
}

我想在我的 ejs 文件中调用 hello() 但它返回一个错误。

你好没有定义

这是我的 ejs 文件

<script src="path/main.js"></script>
<%= hello() %>   ***this is where the error occurs.

如果有人知道如何解决这个问题,请告诉我。我感谢您的所有帮助。:)

标签: javascriptejs

解决方案


你有两个 JavaScript 程序。

一个渲染模板。另一个在从渲染输出创建的网页中运行。

您需要将该功能加载到前者中。

通过在模板的输出中生成脚本元素,您将其加载到后者中。


推荐阅读