【问题标题】:React app on gh-pages : can not link to local filesgh-pages 上的 React 应用程序:无法链接到本地​​文件
【发布时间】:2019-02-21 09:23:14
【问题描述】:

如果我创建了一个 react 应用程序,并且在这个应用程序中有一个指向本地 html 文件的链接,一旦这个应用程序在 gh-pages 上发布,由于某些原因说链接不会指向任何地方,它只是将用户重定向到在点击链接之前他所在的页面。

例如:

如果我像这样使用 CRA 创建一个简单的应用程序:

App.js

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

class App extends Component {
  render() {
    return (
      <div className="App">
        I'm the homepage
        <a id='testlink' href='html2.html'> Go to the second page </a>
      </div>
    );
  }
}

export default App;

在公共文件夹中,我创建了一个新的 html 文件“html2.html”,它只是说

I am the second app !

就是这样,一个简单的应用程序应该在单击链接时从 index.html 跳转到 html2.html。那么这个应用程序在使用npm start 测试时运行良好,如果通过npm run build 提供的静态 html 文件启动它运行良好,但是当部署在 gh-pages 上时,突然链接不会指向任何地方。

Here is the app described above deployed on ghpages

解决此问题的一种方法是在 gh-pages 上分别上传所有应用程序,或使用 react 路由器,但我想知道我是否只是遗漏了一些东西。谢谢你的帮助。

【问题讨论】:

    标签: html reactjs href github-pages


    【解决方案1】:

    React 项目上,您应该使用react-router 来处理routes 并更改页面。

    简单示例:

    import React from "react";
    import { BrowserRouter as Router, Route, Link } from "react-router-dom";
    
    const BasicExample = () => (
      <Router>
        <div>
          <ul>
            <li>
              <Link to="/">Home</Link>
            </li>
            <li>
              <Link to="/about">About</Link>
            </li>
            <li>
              <Link to="/topics">Topics</Link>
            </li>
          </ul>
    
          <hr />
    
          <Route exact path="/" component={Home} />
          <Route path="/about" component={About} />
          <Route path="/topics" component={Topics} />
        </div>
      </Router>
    );
    
    const Home = () => (
      <div>
        <h2>Home</h2>
      </div>
    );
    
    const About = () => (
      <div>
        <h2>About</h2>
      </div>
    );
    
    const Topics = ({ match }) => (
      <div>
        <h2>Topics</h2>
        <ul>
          <li>
            <Link to={`${match.url}/rendering`}>Rendering with React</Link>
          </li>
          <li>
            <Link to={`${match.url}/components`}>Components</Link>
          </li>
          <li>
            <Link to={`${match.url}/props-v-state`}>Props v. State</Link>
          </li>
        </ul>
    
        <Route path={`${match.url}/:topicId`} component={Topic} />
        <Route
          exact
          path={match.url}
          render={() => <h3>Please select a topic.</h3>}
        />
      </div>
    );
    
    const Topic = ({ match }) => (
      <div>
        <h3>{match.params.topicId}</h3>
      </div>
    );
    
    export default BasicExample;
    

    【讨论】:

    • 甚至需要重定向到普通的 html 文件反应路由器?在本地测试但没有托管一次时,它在没有反应路由器的情况下工作是否正常?
    • @Hallemon 是的,SPA 项目需要它。并且在真实主机上不起作用是正常的。
    猜你喜欢
    • 2017-07-25
    • 1970-01-01
    • 2018-12-09
    • 2021-08-26
    • 2022-01-26
    • 1970-01-01
    • 2015-12-13
    • 2018-02-02
    • 2017-03-13
    相关资源
    最近更新 更多