场景是这样的,有一个自定义的tag,用到了_ctx.__require,代码片段如下:
var s = (ignore ? ' try {\n' : '') + 'var _compiler = _ctx.__require("' + normalize + '");' + '_output += _compiler(' + ((onlyCtx && w) ? w : (!w ? '_ctx' : '_utils.extend({}, _ctx, ' + w + ')')) + ');\n' + (ignore ? '} catch (e) {}\n' : '');
然后之前使用过设置模板内变量的方法:
swig.setDefaults({ locals: { 'xxx': '11' } });
一直相安无事,最近改了逻辑,去掉了setDefaults locals,结果一刷新页面(该页面使用了上述自定义tag),页面就报错:
TypeError: _ctx.__require is not a function
并且只是重启node服务后首次刷新页面才会出现,再次刷新就不会出现这个错误。。。搞得莫名其妙的,再次把locals set一下,就不会出现这种报错。 更奇特的是,locals不能是一个空对象{}, 必须至少有一对key:value。
暂时还不明确具体原因。