我正在使用nestJs 框架开发一个MVC 应用程序,并且我使用了hbs 模板引擎。
根据documentation我必须使用这个配置来让nestjs 能够提供 View :
async function bootstrap() {
const app = await NestFactory.create(ApplicationModule);
app.useStaticAssets(join(__dirname, '..', 'public'));
app.setBaseViewsDir(join(__dirname, '..', 'views'));
app.setViewEngine('hbs');
await app.listen(3000);
}
此配置假定所有 View 都位于一个目录( View )中,但如果每个模块都有自己的 View 怎么办?
最佳答案
自 v5.7.0
您可以设置目录数组:
app.setBaseViewsDir([
join(__dirname, '..', 'users/views'),
join(__dirname, '..', 'books/views'),
]);
v5.7.0 之前
express 可以set an array of base path directories :
A directory or an array of directories for the application's views. If an array, the views are looked up in the order they occur in the array.
但是,nest.js 中的类型不允许使用数组,请参阅 issue .我创建了一个 pull request ,这将改变这一点。
在合并拉取请求之前,您可以这样做:
app.setBaseViewsDir([
join(__dirname, '..', 'users/views'),
join(__dirname, '..', 'books/views'),
] as any);
合并拉取请求后,您可以删除 as any
。
关于node.js - NestJs:多 View 目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54098773/