找不到路由

酒神

我有一个问题,当我启动 React 应用程序时,我能够导航到所有路线。当我点击浏览器中的刷新按钮时会出现问题,我收到一条错误消息,指出找不到路由。

以下是路由的配置方式:Index.js

import SearchByRequestId from '../components/SearchByRequestId';
import ErrorsByDate from '../components/ErrorsByDate';
import PayerProperties from '../components/PayerProperties';
import BucketCreator from '../components/BucketCreator';
import EDI275Component from '../components/EDI275Component';

export const SEARCH_BY_REQUEST_ID = 'SEARCH_BY_REQUEST_ID';
export const ERRORS_BY_DATE = 'ERRORS_BY_DATE';
export const PAYER_PROPERTIES = 'PAYER_PROPERTIES';
export const BUCKET_VIEWER = 'BUCKET_VIEWER';
export const EDI_275_VIEWER = 'EDI_275_VIEWER';
export const DEFAULT_ROUTE_VIEWER = 'DEFAULT_ROUTE_VIEWER';

export const PERMISSIONS = {
  MA_ADMIN: '7462'
};



export const REQUEST_DETAILS_ROUTE = {
  name: SEARCH_BY_REQUEST_ID,
  tabName: 'Search By Request Id',
  pathname: '/search_by_id.html',
  component: SearchByRequestId
};

export const ERRORS_BY_DATE_ROUTE = {
  name: ERRORS_BY_DATE,
  tabName: 'Errors By Date',
  pathname: '/errors_by_date.html',
  component: ErrorsByDate
};

export const PAYER_PROPERTIES_ROUTE = {
  name: PAYER_PROPERTIES,
  tabName: 'Payer Properties',
  pathname: '/payer_properties.html',
  component: PayerProperties
};

export const BUCKET_VIEWER_ROUTE = {
  name: BUCKET_VIEWER,
  tabName: 'Create Bucket JSON',
  pathname: '/bucket_creator.html',
  component: BucketCreator
};

export const EDI_275_ROUTE = {
  name: EDI_275_VIEWER,
  tabName: 'Create EDI 275',
  pathname: '/edi_275.html',
  component: EDI275Component
};

export const ALL_ROUTES = [
  REQUEST_DETAILS_ROUTE,
  ERRORS_BY_DATE_ROUTE,
  PAYER_PROPERTIES_ROUTE,
  BUCKET_VIEWER_ROUTE,
  EDI_275_ROUTE
];

export const getAllPermissions = () => Object.keys(PERMISSIONS).map(key => PERMISSIONS[key]);

Page.js 将上面指定的所有路由呈现为选项卡式菜单。这部分就像一个魅力:Page.js

import React, { Fragment, PureComponent, Component } from 'react';
import { Route, Switch, Redirect, withRouter } from 'react-router-dom';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';

import { PropTypes } from 'prop-types';
import { push } from 'connected-react-router';
import { Nav, NavItem, NavLink } from 'reactstrap';
import { authorize, notifications } from '../common/hoc';
import Dashboard from './Dashboard';

import { getAllPermissions, ALL_ROUTES } from '../routes';

class App extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {};
  }

  render() {
    const { routeInfos, push, currentPathname } = this.props;
    const toPath = routeInfos && routeInfos.length > 0 ? routeInfos[0].pathname : '/error.html';
    const tabs = routeInfos.map(routeInfo => (
      <NavItem key={routeInfo.pathname}>
        <NavLink
          href="#"
          active={routeInfo.pathname === currentPathname}
          onClick={evt => {
            push(routeInfo.pathname);
            evt.preventDefault();
            evt.stopPropagation();
          }}
        >
          {routeInfo.tabName}
        </NavLink>
      </NavItem>
    ));
    const routes = routeInfos.map(routeInfo => (
      <Route key={routeInfo.pathname} exact path={routeInfo.pathname} component={routeInfo.component} />
    ));

    return (
      <Fragment>
        {toPath !== '/error.html' && <Dashboard />}
        <Nav className="mt--1 mb-3" tabs>
          {tabs}
        </Nav>
        <Switch>
          {routes}
          {toPath !== '/error.html' && <Redirect from="/" to={toPath} />}
          <Route component={() => <div>Error Loading page</div>} />
          {/* <Route exact path="/" component={<Dashboard />} /> */}
        </Switch>
      </Fragment>
    );
  }
}

App.propTypes = {
  currentPathname: PropTypes.string,
  routeInfos: PropTypes.array,
  push: PropTypes.func
};

const mapStateToProps = state => {
  const {
    router: {
      location: { pathname: currentPathname }
    }
  } = state.toJS();
  return { routeInfos: ALL_ROUTES, currentPathname };
};

const mapDispatchToProps = dispatch =>
  bindActionCreators(
    {
      push
    },
    dispatch
  );

export default withRouter(
  connect(
    mapStateToProps,
    mapDispatchToProps
  )(notifications()(authorize(getAllPermissions)(App)))
);

当页面最初呈现以下 URL 时,http://localhost:3000/search_by_id.html起作用。当我在浏览器中点击相同 URL 的刷新按钮时,我收到以下错误消息:

无法获取 /search_by_id.html

马尔科·格雷萨克

您需要配置您的服务器以正确地为前端路由器创建的路由提供服务。

从 Create React App docs: Serving Apps with Client-Side Routing查看这篇文章即使您没有使用 Create React App,它也可能有助于解释如何配置资产服务器。它们涵盖了很多服务器配置,但如果您的配置不在列表中,您可以尝试搜索“前端路由器配置”。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

MiniTest-找不到路由

来自分类Dev

找不到404 laravel路由

来自分类Dev

找不到Web API路由?

来自分类Dev

Laravel 5.6 - 找不到路由

来自分类Dev

WebAPI中的属性路由:找不到资源

来自分类Dev

API路由抛出“找不到资源”错误

来自分类Dev

fosuserbundle找不到“ GET / login /”的路由

来自分类Dev

在Symfony中找不到字体文件的路由

来自分类Dev

Symfony 2-找不到路由错误

来自分类Dev

找不到Web API路由404

来自分类Dev

找不到快递路由器

来自分类Dev

Windows上的symfony找不到路由错误

来自分类Dev

找不到网络api路由属性

来自分类Dev

Nginx安装后找不到Laravel路由

来自分类Dev

Laravel-记录“找不到路由”

来自分类Dev

反应路由找不到匹配的页面

来自分类Dev

找不到htaccess路由返回404

来自分类Dev

Codeigniter找不到路由定义的位置

来自分类Dev

找不到Laravel路由404错误

来自分类Dev

Rails路由给文件找不到错误

来自分类Dev

找不到页面加载更深的Laravel路由

来自分类Dev

在RestAdapter上找不到错误路由

来自分类Dev

从我的域中找不到Laravel路由资源

来自分类Dev

在 Laravel 路由中找不到页面错误

来自分类Dev

路由器找不到组件

来自分类Dev

Url.Link():使用属性路由时找不到路由

来自分类Dev

在rspec测试中找不到现有路由的路由

来自分类Dev

Symfony和React路由器,找不到路由

来自分类Dev

找不到路由404错误,但是laravel中存在路由