如何在 EJS 客户端使用部件? 我正在使用 express,我想在服务器端和客户端之间共享相同的模板。 我已经将相同的模板编译为客户端代码,但它无法识别部分函数。
ReferenceError: ejs:38
36| <body>
37|
>> 38| <%- partial('header') %>
39| <div class="container">
40| <%- body %>
41| </div> <!-- /container -->
partial is not defined
我认为包括(一年前的部分)在客户端不起作用。您仍然可以尝试编写支持它们的实现,但这会非常困难。就我而言,我只想在客户端禁用它们。添加此行:
source = source.replace(/<% include.+%>/g, "");
做到了。它位于:
EJS.Compiler = function(source, left) {
this.pre_cmd = ['var ___ViewO = [];'];
this.post_cmd = new Array();
this.source = ' ';
if (source != null)
{
if (typeof source == 'string')
{
source = source.replace(/rn/g, "n");
source = source.replace(/r/g, "n");
// Just ignore the includes
source = source.replace(/<% include.+%>/g, "");
this.source = source;
} else if (source.innerHTML){
this.source = source.innerHTML;
}
当然,它离最佳解决方案还很远,但它使我的模板在服务器和客户端都可以工作。就我而言,我不需要在客户端执行此包含。