node.js - NestJs:多 View 目录

标签 node.js typescript express nestjs

我正在使用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/

相关文章:

Typescript:映射类型,使除所选属性之外的每个属性均为只读

javascript - 在 ionic 3 上的 ion-nav 上的 StatusBar

node.js - Express 401 重定向不会自动发生

node.js - 如何访问 basicAuth 内的快速路由

javascript - 是否可以在 Node js 中执行一组包括系统重启的步骤?

node.js - 如何同步请求?

typescript - Partial<SomeClass> 但没有方法?

jquery - 如何在 Express 路由处理程序中进行 socket.broadcast.emit?

node.js - Heroku 部署 - "Push rejected, failed to compile Node.js app"

javascript - 如何使用每个或循环解析内部多个 <p> 标签文本?