Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 神秘的一行,用于在网页包中要求html模板:templates.keys().forEach(模板)?_Javascript_Angularjs_Webpack - Fatal编程技术网

Javascript 神秘的一行,用于在网页包中要求html模板:templates.keys().forEach(模板)?

Javascript 神秘的一行,用于在网页包中要求html模板:templates.keys().forEach(模板)?,javascript,angularjs,webpack,Javascript,Angularjs,Webpack,我想使用ngTemplate loader和html template loader将html模板导入Webpack包,并在另一个项目中找到了两行用于此目的的代码: const templates = require.context(__dirname, true, /\.html$/); templates.keys().forEach(templates); 第一行我很清楚-它递归地需要当前目录下的所有html文件,并将它们添加到$templateCache中 但第二行对我来说完全是一片迷

我想使用ngTemplate loader和html template loader将html模板导入Webpack包,并在另一个项目中找到了两行用于此目的的代码:

const templates = require.context(__dirname, true, /\.html$/);
templates.keys().forEach(templates);
第一行我很清楚-它递归地需要当前目录下的所有html文件,并将它们添加到$templateCache中

但第二行对我来说完全是一片迷雾。这有什么意义?

模板。keys()
将返回匹配文件的路径数组:

['./file1.js', './file2.js'].forEach(templates);
然后,
forEach
将为每个文件调用
templates
函数(即
require
,上下文设置为
\uu dirname
):

templates('./file1.js');
templates('./file2.js');
或:


第一行不需要html文件。它通过上下文创建
require
函数。
require('./file1.js'); // this files will be searched relative to __dirname
require('./file2.js');