神秘的行,用于在 Webpack 中需要 html 模板:templates.keys().forEach(templa
Mysterious line, used to require html templates in Webpack: templates.keys().forEach(templates)?
我想使用 ngTemplate-loader 和 html-template-loader 将 html 模板导入到 Webpack bundle 中,并在另一个项目中找到了用于该目的的两行代码:
const templates = require.context(__dirname, true, /'.html$/);
templates.keys().forEach(templates);
第一行对我来说很清楚 - 它递归地需要当前目录下的所有 html 文件,将它们添加到$templateCache。
但第二行对我来说完全是迷雾。它的意义何在?
templates.keys()
将返回匹配文件的路径数组:
['./file1.js', './file2.js'].forEach(templates);
然后forEach
将为每个文件调用templates
函数(在上下文设置为 __dirname
的情况下require
):
templates('./file1.js');
templates('./file2.js');
或:
require('./file1.js'); // this files will be searched relative to __dirname
require('./file2.js');
相关文章:
- Dojo不解析自定义小部件的模板html中的小部件声明性
- 从Javascript和Php变量创建Html模板文档
- 用JavaScript替换Android WebView模板上的html文本
- 在哪里存储HTML模板以在Backbone.js中使用
- 在树枝模板上从数据库中解压缩HTML字符串
- 在angularjs中将html项插入到现有模板HTML中有什么奇怪的方法
- 引用Cordova/Onsen中的模板HTML元素
- 为 json 数据制作模板 html
- 无法在Angular指令的Karma测试中加载模板HTML文件
- 理解angularjs中的路由和模板html,简单的例子是可能的
- 在应用程序代码上运行Google Closure编译器后,更新AngularJS模板HTML
- Sammy.js如何禁用缓存模板html
- AngularJS如何异步遍历Angular模板(HTML+AngularJS指令)
- 模板html/javascript/css的iphone.需要在UIWebView中运行它
- 如何在angular js中把自定义指令包含到另一个自定义指令的模板(html)中
- 为什么我的ui-view不显示我的模板html文本?
- 是否有任何方式如何下载引导模板(HTML)从他们所做的,并在他们的官方网站上显示为例
- Angular JS-点击按钮,从指令加载一个模板html
- Knockout JS模板HTML不能正确渲染
- 使用text.js与require.js我如何创建一个模板html文件,但单独抓取每个脚本模板