Nat*_*lur 6 javascript express typescript reactjs
我在应用程序中使用express - next.js,并通过res.locals将一些配置从服务器传递到客户端,我的项目位于打字稿中,我使用 "@types/next": "^8.0. 6”。
问题:打字稿说"Property 'locals' does not exist on type 'ServerResponse | Partial<ServerResponse>'."
预期答案: - 一种覆盖“NextContext.res”或“NextResponse”以等于而Express.res
不是http.ServerResponse
- 一种覆盖 NextResponse 以添加的方法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,
}
}
Run Code Online (Sandbox Code Playgroud)
小智 0
对于快速解决方案,这对我有用:
import { Response } from 'express';
MyDocument.getInitialProps = async (ctx) => {
const { locals } = (ctx.res as Response);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3521 次 |
最近记录: |