Reactjs 使用withRouter和自定义服务器的浅层路由不工作
使用withRouter作为自定义服务器的包装器,浅层路由似乎不起作用 我目前使用此方法更改路线:Reactjs 使用withRouter和自定义服务器的浅层路由不工作,reactjs,next.js,server-side-rendering,Reactjs,Next.js,Server Side Rendering,使用withRouter作为自定义服务器的包装器,浅层路由似乎不起作用 我目前使用此方法更改路线: this.props.router.push({ pathname: currentPath, query: currentQuery, }); 路由器道具来自使用withRouter包装我的类组件 也不知道该把浅旗放在哪里。因此,我切换到文档中提到的方法: this.props.router.push('/post/[pid]?hello=123', '/post/abc?he
this.props.router.push({
pathname: currentPath,
query: currentQuery,
});
路由器道具来自使用withRouter包装我的类组件
也不知道该把浅旗放在哪里。因此,我切换到文档中提到的方法:
this.props.router.push('/post/[pid]?hello=123', '/post/abc?hello=123', { shallow: true })
所以我手动操作,但我开始得到404
http://localhost:3000/_next/static/development/pages/search/%5Btype%5D/%5Bcat%5D/%5Barea%5D.js net::ERR_ABORTED 404 (Not Found)
解码:
"http://localhost:3000/_next/static/development/pages/search/[type]/[cat]/[area].js"
我试着用:type代替[type],但也没用
服务器上的配置方式如下:
if ('/search/:type/:cat/:area' === route.path) {
return app.render(req, res, route.page);
}
文件夹结构:
/pages/search/index.js
我认为这种结构与问题有关,因为它位于index.js中,而不仅仅是pages文件夹中的普通文件
它不应该在更改路线时重新加载页面,这是我试图完成的主要任务。
我正在实现SSR分页,并计划使用浅层路由在客户端而不是服务器上进行页面更改。意思是只在第一次加载时实现SSR,让用户保持在不刷新状态
我也试过:
server.get('/search/:type/:cat/:area', (req, res) => {
console.log("reached here..."); // this gets logged
return app.render(
req,
res,
'/search/[type]/[cat]/[area]',
req.params
);
});
但是我得到了404,页面现在不在那里
这也不起作用:
this.props.router.push(
`/search/[type]/[cat]/[area]?${stringifyQs(currentQuery)}`,
{
pathname: currentPath,
query: currentQuery,
},
{ shallow: true }
);
这应该起作用:
server.js
server.get('/search/:type/:cat/:area',(req,res)=>{
返回app.render(请求,res,“/search”{
…请求参数,
…请求查询,
});
});
pages/search/index.js
props.router.push(
“/search?type=foo&cat=bar&area=baz&counter=10”,
“/search/foo/bar/baz?counter=10”,
{浅:真}
);
由于文件系统中没有
页面/search/[type]/[cat]/[area].js
页面,因此可能会出现404
错误的链接问题。您必须导出该文件中的React组件,该组件将呈现该路由路径的页面。很抱歉,我很难理解在this.props.router.push('/post/[pid]?hello=123','/post/abc?hello=123',{shallow:true})的情况下,您究竟想用shallow-routing.shallow路由实现什么
仅当您已经在路径/post/abc
上并且尝试使用查询hello=123
@subashMahapatra更新URL路径时才起作用。我将为您提供更多详细信息。我使用SSR,搜索页面存在于/pages/search/index.js中,因此您提到的文件将不存在。很遗憾,我们还可以尝试解决此问题吗?参数不会改变,只是查询而已。等一下,你是不是想像这样更新URL<代码>/search?type=some type&cat=asdfsd&area=some area?