首页 > 解决方案 > Fastify 模式下 NestJS 动态模板渲染 - res.render 不是函数

问题描述

我觉得Model-View-Controller @ docs.nestjs.com没有涵盖这个用例 - 有“动态模板渲染”部分 + Fastify 但两者结合在一起似乎效果不佳。

我尝试了以下代码段:

import { Get, Res } from '@nestjs/common'

@Get()
index(@Res() res) {
  if (isFoo()) {
    return res.render('template1.html', {var1: 'foo'})
  } else {
    return res.render('template2.html', {var2: 'bar'})
  }
}

它失败了

TypeError:res.render 不是函数

有什么办法可以解决吗?

为了清楚起见,我不使用的原因@Render是因为我需要该操作中的条件逻辑。

标签: nestjsfastifynestjs-fastify

解决方案


res.render()是快递方式。对于 Fastify,您需要安装point-of-view和使用res.view()


推荐阅读