【问题标题】:nextjs pass an object from getserversideprops to page functionnextjs 将一个对象从 getserversideprops 传递给页面函数
【发布时间】:2022-11-25 10:46:35
【问题描述】:

在下面的代码中,当我通过 props 在函数 getserversideprops 中返回一个类对象时,在页面函数中变量是未定义的,就像下面的代码一样

`

export default function cars(obj){
    return <h1>counter: {obj.counter}</h1> // obj is undefined, why??
}
export async function getServerSideProps({req,res}){
    class Counter{
        constructor(){
            this.counter = 22
        }
    }
    var counter = new Counter()

    return {
        props:
        {
            obj:JSON.stringify(counter)
        }
    }
}

`

我期望页面参数对象将具有对象计数器而不是未定义的

【问题讨论】:

    标签: next.js compiler-errors getserversideprops


    【解决方案1】:
    export default function cars(props){
    return <h1>counter: {JSON.parse(props.obj).counter}</h1>
    

    }

    【讨论】:

      猜你喜欢
      • 2020-08-09
      • 2021-03-18
      • 2023-01-24
      • 2021-05-13
      • 2021-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多