跳到主要内容
CNode

ejs模板里可以使用require来动态加载东西么?

Jjtyjty99999发布于 12 年前最后回复 8 年前
476120

比如

<%var selector=require('../conf/selectors/'+m+'.json')[selectors]%>

会报 require is not defined 。是因为require调用的是nativeModule.require 但是模板引擎中没有么?

查看回复

回复 (4)

N
nihgwu#112 年前

express中 app.locals.require = require;

A
alsotang#212 年前
引用 nihgwuexpress中 app.locals.require = require;

这样也行!?

长见识了。。

H
hello-guoguo#38 年前
引用 nihgwuexpress中 app.locals.require = require;

@nihgwu 我这样用,还是报 require is not defined.

express中: 111.png

ejs模板中: NMPSY8WAWW({T_8EQ52@W.png

控制台报错: 2222.png

H
hello-guoguo#48 年前
引用 alsotang这样也行!? 长见识了。。

@alsotang 我也遇到同样的问题了,你解决了没

参与回复

登录后即可参与回复。登录