有没有办法让你的Jade模板的HTML输出得到美化?类似于Express app.locals.pretty = true;
的东西?如果相关,请使用koa-router
和koa-views
。
server.js
const koa = require('koa');
const views = require('koa-views');
const serve = require('koa-static');
const router = require('./routes');
const app = koa();
app.use(serve(`${__dirname}/public`));
app.use(views(`${__dirname}/views`, { extension: 'jade' }));
app.use(router.routes());
app.listen(3000, () => {
console.log('Server listening at http://localhost:3000');
});
路由/ index.js
const router = require('koa-router')();
router.get('/', function *() {
yield this.render('index');
});
module.exports = router;
视图/ index.jade
html
head
title Hello
meta(charset='utf-8')
meta(name='viewport', content='width=device-width, initial-scale=1.0')
link(type='text/css', rel='stylesheet', href='css/style.css')
body
h1 Hi
答案 0 :(得分:0)
app.use(views(`${__dirname}/views`, { extension: 'jade', pretty:true}));
应该做的诀窍...... 另一方面,我使用koa-jade而不是koa-views。我目前的代码如下:
var Jade = require('koa-jade');
var jade = new Jade({
viewPath: path.resolve(__dirname,"jade"),
debug: true,
pretty: true,
compileDebug: true,
basedir: path.resolve(__dirname,"jade"),
app:app
})
在生产模式中我只是设置为假...
HTH