使用 react-router-dom 时,Netlify 无法识别 URL 参数
Netlify does not recognize the URL params when using react-router-dom
我正在创建一个使用反应路由器的反应应用程序。我正在使用路由器来匹配 :/bankName-:credit
之类的路径,它在本地开发中运行良好。我的应用程序唯一需要的路径是 :/bankName-:credit
,其他所有路径都会达到 404
。
但是当我将此应用程序部署到 netlify 时,默认情况下它会转到 /
并显示自定义 404
。这一切都很好。但是现在如果我尝试去 /hdfc-500
那么它会给出一个 netlify not found 消息 page not found
.
我尝试使用 netlify docs 中提到的 _redirects
,但这不起作用。
这是我的路线:-
App.js
<Route path='/:bankCode-:credit' component={NestedRoutes} />
<Route component={NotFound} />
这是我的 NestedRoutes
组件:-
const NestedRoutes = ({ match }) => (
<Suspense fallback={<LinearProgress />}>
<Switch>
<Route exact path={`${match.path}/sc-generate`} component={SCGenerate} />
<Route exact path='/:bankCode-:credit' component={Home} />
<Route component={NotFound} />
</Switch>
</Suspense>
)
我在 _redirects
文件中使用以下代码:-
/* /:bankCode-:credit
但它会尝试与 /:bankCode-:credit
完全匹配
我应该怎么做才能解决这个问题?
我在这里重现了你的问题https://codesandbox.io/s/trusting-frost-ls353
解决方法很简单,将名为 _redirects
的文件添加到您的 public 文件夹中,内容为
/* /index.html 200
您可以找到有关此 link 的更多信息。 https://www.slightedgecoder.com/2018/12/18/page-not-found-on-netlify-with-react-router/
我在 在 netlify 中传递 url 参数时遇到了这个问题,通过删除“.”得到了解决。
index.html 文件夹中 index.html 文件中的 href 和 src 链接之前。
希望这对某人有所帮助。
我正在创建一个使用反应路由器的反应应用程序。我正在使用路由器来匹配 :/bankName-:credit
之类的路径,它在本地开发中运行良好。我的应用程序唯一需要的路径是 :/bankName-:credit
,其他所有路径都会达到 404
。
但是当我将此应用程序部署到 netlify 时,默认情况下它会转到 /
并显示自定义 404
。这一切都很好。但是现在如果我尝试去 /hdfc-500
那么它会给出一个 netlify not found 消息 page not found
.
我尝试使用 netlify docs 中提到的 _redirects
,但这不起作用。
这是我的路线:-
App.js
<Route path='/:bankCode-:credit' component={NestedRoutes} />
<Route component={NotFound} />
这是我的 NestedRoutes
组件:-
const NestedRoutes = ({ match }) => (
<Suspense fallback={<LinearProgress />}>
<Switch>
<Route exact path={`${match.path}/sc-generate`} component={SCGenerate} />
<Route exact path='/:bankCode-:credit' component={Home} />
<Route component={NotFound} />
</Switch>
</Suspense>
)
我在 _redirects
文件中使用以下代码:-
/* /:bankCode-:credit
但它会尝试与 /:bankCode-:credit
我应该怎么做才能解决这个问题?
我在这里重现了你的问题https://codesandbox.io/s/trusting-frost-ls353
解决方法很简单,将名为 _redirects
的文件添加到您的 public 文件夹中,内容为
/* /index.html 200
您可以找到有关此 link 的更多信息。 https://www.slightedgecoder.com/2018/12/18/page-not-found-on-netlify-with-react-router/
我在 在 netlify 中传递 url 参数时遇到了这个问题,通过删除“.”得到了解决。 index.html 文件夹中 index.html 文件中的 href 和 src 链接之前。
希望这对某人有所帮助。