简体   繁体   English

部署时反应路由器无匹配 404 不起作用?

[英]React router No Match 404 not working when deployed?

I added a No Match 404 error page to my website, and it works fine when I go to the wrong path on localhost, but after I deploy and go to an invalid path like .../invalidpath I get the default netlify page not found error.我在我的网站上添加了一个 No Match 404 错误页面,当我转到 localhost 上的错误路径时它工作正常,但是在我部署并转到一个无效路径后,例如 .../invalidpath 我得到了默认的 netlify 页面未找到错误。 Below is my App.js component:下面是我的 App.js 组件:

import React from 'react';
import { Switch, Route } from 'react-router-dom';
import Navigation from './components/navigation/Navigation';
import Home from './pages/home/Home';
import Projects from './pages/projects/Projects';
import Contact from './pages/contact/Contact';
import NoMatch from './pages/404page/404Page';

import './App.scss';

function App() {
  return (
    <div className='app'>
      <Navigation />
      <Switch>
        <Route exact path='/' component={Home} />
        <Route path='/projects' component={Projects} />
        <Route path='/contact' component={Contact} />
        <Route path='*' component={NoMatch} />
      </Switch>
    </div>
  );
}

export default App;

If you've built the app with create-react-app and you're deploying to Netlify, you need to add a _redirect file inside the public directory of your project in order to support client side routing (React Router).如果你已经用create-react-app构建了应用create-react-app并且你正在部署到 Netlify,你需要在你的项目的public目录中添加一个_redirect文件以支持客户端路由(反应路由器)。 It should have this inside:里面应该有这个:

/*  /index.html  200

Details here详情在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM