首页 > express+ejs如何使两ejs在同一页面显示?

express+ejs如何使两ejs在同一页面显示?

很初级的问题:

有两个功能:发布评论和展示评论,想要评论和发布评论在同一个页面。
已经写好(数据库方法)bbs_all(包括Comment和Bbs两个对象)
(发布评论)comment.js和comment.ejs
(展示评论)bbs.js和bbs.ejs

但当我在bbs.ejs中插入 时,打开网页时,提示
TypeError: Object function Comment(comment){
this.mycomment = comment.mycomment;
} has no method 'readComment'

怎么才能将comment.ejs的部分放入bbs.ejs里(是否可行)?这种写法为什么不行?

bbs_all.js


function Comment(comment){ this.mycomment = comment.mycomment; }; function Bbs() {}; module.exports = Bbs; module.exports = Comment; pool.getConnection(function(err, connection) { var useDbSql = "USE " + DB_NAME; connection.query(useDbSql, function (err) { if (err) { console.log("USE Error: " + err.message); return; } console.log('USE succeed'); }); //保存评论 User.prototype.save = function save(callback) { var user = { username: this.username }; var insertTip_Sql = "INSERT INTO bbs(comment) VALUES(?)"; connection.query(insertTip_Sql, [user.username], function (err,result) { if (err) { console.log("insertTip_Sql Error: " + err.message); return; } console.log("invoked[save]"); callback(err,result); }); }; //获取评论 Bbs.prototype.readComment = function (callback){ pool.query('SELECT comment FROM bbs', function(err, result) { console.log("invoked[readComment]"); callback(err, result); }); }; });

可行!必须可行!

<%- include xxx.ejs %>

在ejs中引入其他的ejs页面

如果配置了ejs模板,也可以省略.ejs

<%- include xxx %>

xxx为页面的名字

【热门文章】
【热门文章】