我是Next.js的新手,并且很难使帖子ID从索引页面传递到帖子页面getInitialProps()函数。正在使用链接组件请求页面。
最终目标是将帖子ID放入post.js的getInitialProps函数中,以便像以下这样请求正确的json文件:
https://www.example.com/menu/100.json <-id = 100
JSON API端点都可以从浏览器正常工作,并且索引页面在nextjs中正常工作。
到目前为止,我已经尝试了一些打印语句来查找传递此Link href属性的位置,但是我仍无法访问href中的“ itemid”。
有3个主要文件可以重现此内容:
index.js
import Layout from '../components/MyLayout.js'
import Link from 'next/link'
import fetch from 'isomorphic-unfetch'
const Index = (props) => (
<Layout>
<h1>Pages</h1>
<ul>
{props.results.map(result => (
<li key={result.id}>
<Link as={`/${result.slug}`} href={`/post?itemid=${result.id}`}>
<a>{result.title}</a>
</Link>
</li>
))}
</ul>
</Layout>
)
Index.getInitialProps = async function() {
const res = await fetch('https://www.example.com/menu.json')
const data = await res.json()
return {
results: data['data'].map(data => data)
}
}
export default Index
server.js
const express = require('express')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
app
.prepare()
.then(() => {
const server = express()
server.get('/:id*', (req, res) => {
const actualPage = '/post'
const queryParams = { slug: req.params.id + req.params[0]}
app.render(req, res, actualPage, queryParams)
})
server.get('*', (req, res) => {
return handle(req, res)
})
server.listen(3000, err => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})
.catch(ex => {
console.error(ex.stack)
process.exit(1)
})
post.js
import { withRouter } from 'next/router'
import Layout from '../components/MyLayout.js'
import fetch from 'isomorphic-unfetch'
const Post = props => (
<Layout>
<h1>{props.result.title}</h1>
<p>{props.result.bodyCopy.replace(/<[/]?p>/g, '')}</p>
<img src={props.result.image} />
</Layout>
)
Post.getInitialProps = async function(context) {
const { itemid } = context.query
const res = await fetch(`https://www.example.com/menu/${itemid}.json`)
const result = await res.json()
return { result }
}
export default withRouter(Post)
这就是我想发生的事情:
用户请求:https://www.example.com/some-page “某些页面”的帖子ID为100
服务器在请求/刷新的页面上同时显示“ some-page”和“ 100”。
服务器对以下对象进行API调用:https://www.example.com/menu/100.json
到目前为止,我得到的最好结果是将API网址设置为:
https://www.example.com/menu/some-page.json
此端点无效,因为ID并未请求该端点。
答案 0 :(得分:0)
请尝试此操作是否按预期工作?
Index.js
import Layout from '../components/MyLayout.js'
import Link from 'next/link'
import fetch from 'isomorphic-unfetch'
const Index = (props) => (
<Layout>
<h1>Pages</h1>
<ul>
{props.results.map(result => (
<li key={result.id}>
<Link as={`/${result.slug}/${result.id}`} href={`/post?itemid=${result.id}`}>
<a>{result.title}</a>
</Link>
</li>
))}
</ul>
</Layout>
)
Index.getInitialProps = async function() {
const res = await fetch('https://www.example.com/menu.json')
const data = await res.json()
return {
results: data['data'].map(data => data)
}
}
export default Index
Server.js
const express = require('express')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
app
.prepare()
.then(() => {
const server = express()
server.get('/:slug/:id*', (req, res) => {
const actualPage = '/post'
const queryParams = { slug: req.params.slug, itemid: req.params.id }
app.render(req, res, actualPage, queryParams)
})
server.get('*', (req, res) => {
return handle(req, res)
})
server.listen(3000, err => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})
.catch(ex => {
console.error(ex.stack)
process.exit(1)
})
Post.js
import { withRouter } from 'next/router'
import Layout from '../components/MyLayout.js'
import fetch from 'isomorphic-unfetch'
const Post = props => (
<Layout>
<h1>{props.result.title}</h1>
<p>{props.result.bodyCopy.replace(/<[/]?p>/g, '')}</p>
<img src={props.result.image} />
</Layout>
)
Post.getInitialProps = async function(context) {
const { itemid } = context.query
const res = await fetch(`https://www.example.com/menu/${itemid}.json`)
const result = await res.json()
return { result }
}
export default withRouter(Post)