Val*_*chy 8 reactjs next.js ssg
我试图在 Next.JS 中生成一个index.html页面getStaticPaths,但每次我尝试这样做时,它都会抛出错误或创建一个index目录而不是文件。
到目前为止,我已经尝试传递:''和in 。我也尝试添加文件,但这也不起作用。'/''index'params.pagetrailingSlash: true,next.config
[page].js我在页面目录中有一个文件。使用此文件,我生成了许多页面,因为该[page].js文件是模板文件。我使用这种模板方法是因为有很多通用组件,每个页面唯一不同的是“主要内容”。想象一下,生成 10 个页面,每个页面都有相同的导航栏和侧面菜单,但内容(文本)不同。因此,我尝试生成 10 个页面,其中之一是页面,但它index.html不是生成文件,而是生成一个目录。我该如何解决这个问题?.htmlindex
export const getStaticPaths = async () => {
return {
paths: [{
params: { // Imagine the following code for another 9 pages
page: 'index' // Only the `index` one has a problem (creates a directory instead of file)
}
}]
};
}
Run Code Online (Sandbox Code Playgroud)
index如果我传递除in之外的任何其他字符串,params.page则会创建一个[my_string].html页面。我希望能够index.html使用 next.js 的getStaticPaths方法创建一个页面。
答案可能有点晚了,但 NextJS 添加了一个可选的捕获所有路由。
您现在可以使用两个括号来命名您的页面[[...slug]]。
在 get Static Props 上,params它将是一个空对象,并且不会有 slug 属性,因此您可以执行此检查:
export const getStaticProps: GetStaticProps<{ data: PageData }> = async ({ params }) => {
const homePage = !!!params.url;
// now do your logic if the page is the home.
Run Code Online (Sandbox Code Playgroud)
请在这里检查我的问题:
https://github.com/vercel/next.js/discussions/34311#discussioncomment-2178744
| 归档时间: |
|
| 查看次数: |
1757 次 |
| 最近记录: |