javascript - 如何从页面的 javascript 文件而不是 html 中访问预编译的 Handlebars 模板?
问题描述
我有一个terrain.html 文件和一个terrain.js。我已经预编译了一个车把模板(templates/favorites.precompiled.js)。我正在尝试在terrain.js 文件中引用收藏夹模板,以便可以注入上下文,然后将其附加到ID 为“输出”的容器中。
我可以在 HTML 中很好地访问模板,但我无法弄清楚我需要做什么才能从 terrain.js 文件中引用它。
<script src="scripts/terrain.js"></script>
<script src="https://cdn.jsdelivr.net/npm/handlebars@latest/dist/handlebars.runtime.js"></script>
<script id="favorites-template" src="partials/favorites.precompiled.js"></script>
<div id="output">
</div>
<script>
console.log(Handlebars.templates)
</script>
这会按预期输出:{favorites: ƒ}
但是,如果我尝试在 terrain.js 中将 Handlebars.templates 打印到控制台,我会得到未定义。我也尝试过指定特定的模板 Handlebars.templates['favorites'] 和 Handlebars.templates['templates/favorites.precompiled'] 等。
解决方案
推荐阅读
- r - 读取数据框列表的 CSV 并转换为数据框列表
- android - RequestPermission 错误:requestCode 只能使用低 16 位
- r - R,系数:3 由于奇点而未定义
- wordpress - 在 WooCommerce 结帐时根据发货国家/地区显示字符串
- html - 添加类 is-sticky 时删除 ::before 和 ::after 伪元素
- android - 无法解决:com.paytm.easypay:easypay:8.5.0-RC-2
- sql - 在 postgres 数据表中创建由几列组成的列
- xcode - OS 10 和 OS11 上的 WKWebView 问题
- php - DB Csv 导出和 Excel 数字格式
- visual-studio - 如何在 Visual Studio 解决方案中切换 2019 和 2017 项目?