下一个JS getInitialProps Axios.Get动态API端点

问题描述 投票:0回答:1

我有一个next.js应用程序,该应用程序对django服务器进行API调用。我现在遇到的问题是使用getInitialProps调用动态内容。请参阅下面的网址示例。基本上,我将如何在getinitialprops中动态拉出主键。

示例:

。herokuapp.com/api/v2/pages/1/?type=projects.ProjectsPage&fields=* vs.herokuapp.com / api / v2 / pages / 2 /?type = projects.ProjectsPage&fields = *

static async getInitialProps(ctx) {
        const resProjects = await axios.get(`...herokuapp.com/api/v2/pages/PK/?type=projects.ProjectsPage&fields=*`);
        return {
            data: data
        }
    }
django django-rest-framework next.js
1个回答
0
投票

假设您正在使用动态路由,则可以通过ctx访问该信息。例如,在/ projects / [id]页面上,您可以访问id,如下所示。

static async getInitialProps(ctx) {
    const id = ctx.query.id;
    //make the api call using id
}

在此说明:https://nextjs.org/docs/routing/dynamic-routes

© www.soinside.com 2019 - 2024. All rights reserved.