javascript - 无法在 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.
如果有人知道如何解决这个问题,请告诉我。我感谢您的所有帮助。:)
解决方案
你有两个 JavaScript 程序。
一个渲染模板。另一个在从渲染输出创建的网页中运行。
您需要将该功能加载到前者中。
通过在模板的输出中生成脚本元素,您将其加载到后者中。
推荐阅读
- typescript - TestCafe 默认与命名类
- r - 在 CentOS 7.4 上 CRAN 安装 devtools 2.0 失败
- java - 将罗马数字转换为十进制值错误
- javascript - 如何使用 javascript 创建离线网络应用程序
- batch-file - 禁用 eol,将 delims 设置为换行或换行以启用按行号进行标记化
- java - 如何在 Maven 项目中调用测试资源上的 aa 文件?
- python - 在 PySpark 中使用多处理时 sparkSession 出错
- c# - C#在jquery之后抓取正确的网页内容
- c# - 按搜索字符串过滤 CollectionViewSource - 绑定到 itemscontrol (WPF MVVM)
- json - 使用 subscribe 或 promise 调用 JSON