我试图在hosted url (部署我的构建的地方).but基础上发送一些文本,我得到的是错误ReferenceError:位置未定义--是我的代码
https://codesandbox.io/s/laughing-mendel-pf54l?file=/pages/index.js
export const getStaticProps = async ({ preview = false, previewData = {} }) => {
return {
revalidate: 200,
props: {
//req.host
name: location.hostname == "www.google.com" ? "Hello" : "ccccc"
}
};
};发布于 2021-05-24 10:38:24
您能显示您的导入吗?因为它可能是从'next/client'导入路由器。
假设您正在使用基于功能的组件。
您需要按以下方式导入路由器:
import {useRouter} from "next/router";在你的身体中:
const router = useRouter();发布于 2021-01-21 01:20:57
getStaticProps()是在构建时在Node.js中执行的,它没有location全局对象-- Location是浏览器API的一部分。此外,由于代码是在构建时执行的,所以URL尚不清楚。
getStaticProps更改为getServerSideProps (见文件)。这意味着在运行时对每个请求分别调用该函数。context的getServerSideProps对象中,取出Node.js 对象。Host头。export const getServerSideProps = async ({ req }) => {
return {
props: {
name: req.headers.host === "www.google.com" ? "Hello" : "ccccc"
}
};
};注:
==更改为===,因为一般建议使用后者。前者由于无声类型转换而产生一些意想不到的结果。revalidate,因为这不适用于getServerSideProps()。https://stackoverflow.com/questions/65819812
复制相似问题