为了账号安全,请及时绑定邮箱和手机立即绑定

如何在next.js中使用带有打字稿的express res.locals

如何在next.js中使用带有打字稿的express res.locals

慕桂英4014372 2021-09-04 17:40:31
我使用的快递-在我的应用next.js,我通过一些配置给客户端从服务器通过res.locals,我的项目是打字稿,我使用“@类型/下一个”:“^ 8.0。 6”。问题:打字稿说 "Property 'locals' does not exist on type 'ServerResponse | Partial<ServerResponse>'."预期答案: - 一种将“NextContext.res”或“NextResponse”覆盖为等于Express.res而不是的方法http.ServerResponse - 一种将 NextResponse 覆盖为 add 的方法locals: any | {x:strong}。这是我们所拥有的以及如何重现的简化示例:import React from 'react';import Document, { Head, Main, NextScript } from 'next/document';type Props = {  locals: {};}export default class MyDocument extends Document<Props> {  render() {    const { locals } = this.props;    return (      <html>        <Head>          <ComponentUsingLocalsInfo locals={locals} />        </Head>        <body>          <Main />          <NextScript />        </body>      </html>    )  }}MyDocument.getInitialProps = async (ctx) => {  const { locals } = ctx.res;  const initialProps = await Document.getInitialProps(ctx);  return {    ...initialProps,    locals,  }}
查看完整描述

2 回答

?
波斯汪

TA贡献1811条经验 获得超4个赞

对于快速解决方案,这对我有用:


    import { Response } from 'express';


    MyDocument.getInitialProps = async (ctx) => {


        const { locals } = (ctx.res as Response);


    }


查看完整回答
反对 回复 2021-09-04
?
慕尼黑8549860

TA贡献1818条经验 获得超11个赞

你总是可以像这样创建一个自定义界面:


export interface Custom_Initial_Props extends Omit<NextPageContext, 'res'> {

    res: Express.Request;

}


查看完整回答
反对 回复 2021-09-04
  • 2 回答
  • 0 关注
  • 169 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信