刷新或直接单击链接时找不到 React.js 页面

IT技术 reactjs react-router
2021-05-13 19:51:51

我在此链接上有一个react项目(https://notchy-cross.000webhostapp.com)。如果你向下滚动,你会看到一个名为(产品类别), https://i.stack.imgur.com/QDHh2.png 当你点击其中一个,它需要你的https://notchy-cross.000webhostapp .com/mugshttps://notty-cross.000webhostapp.com/vases但问题是如果你点击上面的链接,它会给你 404 页面,它只会在你点击主页上的链接时显示内容,(https://notchy-cross.000webhostapp.com),一旦你刷新页面你得到404页面。那为什么会这样呢??

应用程序.js

import React from "react";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import "./App.css";
import Navbar from "./Components/Navbar";
import Home from "./Components/pages/Home";
import About from "./Components/pages/About";
import Shop from "./Components/pages/Shop";
import Contact from "./Components/pages/Contact";
import CategoryPage from "./Components/CategoryPage";
import ProductPage from "./Components/pages/ProductPage";
function App() {
 return (
  <Router>
    <Navbar />
    <Switch>
      <Route exact path="/" component={Home} />
      <Route path="/about" component={About} />
      <Route path="/shop" component={Shop} />
      <Route path="/contact" component={Contact} />
      <Route path="/:to">
        <CategoryPage />
      </Route>
    </Switch>
  </Router>
 );
}

export default App;

类别项.js

import React, { Component } from "react";
import { Link } from "react-router-dom";
import "./CateItem.css";
class CateItem extends Component {
render() {
 return (
   <div className="col-12 col-md-4">
     <Link to={`/${this.props.to}`}>
       <div
         className={`cricle text-center ${this.props.to
           ? this.props.to
           : ""}`}
       >
         <div className="circle-desc">
           <img src={`./images/${this.props.to}.png`} alt={this.props.to} />
           <h6>{this.props.to.toUpperCase()}</h6>
         </div>
       </div>
     </Link>
   </div>
  );
 }
}

export default CateItem;
1个回答

我在 GitHub Pages 上使用 BrowseRouter 时遇到了类似的问题。尝试使用 HashRouter 而不是 BrowseRouter,它可能会起作用。

某些主机不像您的浏览器那样支持浏览器历史记录。HashRouter 使用 URL 的哈希部分来使 UI 与 URL 保持同步。

参考文献:https : //create-react-app.dev/docs/deployment/#notes-on-client-side-routing https://www.freecodecamp.org/news/deploy-a-react-app-to- github页面/

import React from "react";
import { HashRouter as Router, Switch, Route } from "react-router- 
dom";
import "./App.css";
import Navbar from "./Components/Navbar";
import Home from "./Components/pages/Home";
import About from "./Components/pages/About";
import Shop from "./Components/pages/Shop";
import Contact from "./Components/pages/Contact";
import CategoryPage from "./Components/CategoryPage";
import ProductPage from "./Components/pages/ProductPage";
function App() {
  return (
    <Router>
    <Navbar />
    <Switch>
      <Route exact path="/" component={Home} />
      <Route path="/about" component={About} />
      <Route path="/shop" component={Shop} />
      <Route path="/contact" component={Contact} />
      <Route path="/:to">
        <CategoryPage />
      </Route>
    </Switch>
  </Router>
 );
}

export default App;