加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – 无法在react app中的组件之间成功路由

发布时间:2020-12-15 20:30:09 所属栏目:百科 来源:网络整理
导读:我有一个静态反应应用程序(这意味着没有服务器端呈现)位于example.com/web-class.gr下.我的问题是,当我使用侧边栏菜单时,我无法在组件之间进行路由. 例如.当我导航到example.com/web-class.gr/css-intermediate时,页面按预期加载.从现在开始,如果我导航到不
我有一个静态反应应用程序(这意味着没有服务器端呈现)位于example.com/web-class.gr下.我的问题是,当我使用侧边栏菜单时,我无法在组件之间进行路由.

例如.当我导航到example.com/web-class.gr/css-intermediate时,页面按预期加载.从现在开始,如果我导航到不同的lessonName,页面将按预期加载.但我也有练习,当我按下菜单中的相应按钮时无法加载.要知道这是我的index.js文件:

import React from 'react';
import { Link as ReactLink } from 'react-router';
import sidebarStore from './Sidebar/SidebarStore';
import lessonValues from '../../lessonValues';
import LessonStore from '../../LessonStore';
import SidebarLink from './Sidebar/SidebarLink';


export default class Sidebar extends React.Component {
  constructor() {
    super();
    this.state = {
      SidebarIsCollapse: sidebarStore.getCurrentState()
    }
    this.NavMdPlaceholderClass = 'hidden-xs col-sm-4 col-md-3 col-lg-3';
  }

  componentWillMount() {
    sidebarStore.on('change',() => {
      this.setState({ SidebarIsCollapse: sidebarStore.getCurrentState() });
      this.ChangeSidebarState();
    });
    this.RenderMainMenu();
  }

  ChangeSidebarState() {
    const NAV_DefaultClasses = "col-sm-4 col-md-3 col-lg-3 ";
    if (this.state.SidebarIsCollapse) {
      this.NavMdPlaceholderClass = NAV_DefaultClasses + "slideInLeft";
    } else {
      this.NavMdPlaceholderClass = NAV_DefaultClasses + "slideOffLeft";
    }
  }

  RenderMainMenu() {
    this.main_menu = [];
    for (let link of lessonValues) {
      let { Id,url,isExercise,title } = link;
      this.main_menu.push(<SidebarLink key={Id} url={url} isExercise={isExercise} title={title}/>);
    }
  }

  render() {
    return (
      <div class={this.NavMdPlaceholderClass} id="nav-md-placeholder">
        <nav id="sidebar">
          <ul id="main-menu">
            <li class="ripple-btn">
                <ReactLink to="/" onClick={this.SetLessonDetails.bind(this)}>
                  <span class="item-align-fix">
                    <i class="glyphicon glyphicon-home" style={{'marginRight': '10px'}}></i>
                    <strong>
                      <span>AΡΧΙΚΗ</span>
                    </strong>
                  </span>
                </ReactLink>
              </li>

              {this.main_menu}
          </ul>
        </nav>
      </div>
    );
  }
}

这是SidebarLink组件文件:

import React from 'react';
import LessonStore from '../../../LessonStore';
import { Link as ReactLink } from 'react-router';

export default class SidebarLink extends React.Component {
  SetPageTitle() {
    LessonStore.setLesson(this.props.url);
  }

  render() {
    let glyphoconType = 'glyphicon ';
    glyphoconType += this.props.isExercise ? 'glyphicon-pencil' : 'glyphicon-ok-sign';
    glyphoconType += ' nav-ico untaken-lesson';

    return (
      <li class="ripple-btn">
        <ReactLink to={this.props.url} onClick={() => this.SetPageTitle()} >
          <span class="item-align-fix">
            <i class={glyphoconType}></i>
              <span>{this.props.title}</span>
          </span>
        </ReactLink>
      </li>
    );
  }
}

但是,如果我手动刷新页面,我可以显示练习页面.但现在我无法导航到任何其他元素.仅当我在侧边栏菜单中单击它并手动刷新页面时.

总结一下:

>课程是动态加载的.我可以在他们之间导航.
>我无法导航到练习.只有当我单击相应的练习并点击刷新按钮时.
>如果我正在观看练习(例如练习-1),我无法导航到任何其他组件.

我使用nginx,以下是我的项目规则:

location ^~ /web-class.gr/ {
        try_files $uri $uri/ =404;
        if (!-e $request_filename){
            rewrite ^(.*)$/web-class.gr/index.html break;
        }
    }

最后这是我的侧边栏组件:

import React from 'react';
import { Link as ReactLink } from 'react-router';
import sidebarStore from './Sidebar/SidebarStore';
import lessonValues from '../../lessonValues';
import LessonStore from '../../LessonStore';
import SidebarLink from './Sidebar/SidebarLink';

// some other helper functions here

  render() {
    return (
      <div class={this.NavMdPlaceholderClass} id="nav-md-placeholder">
        <nav id="sidebar">
          <ul id="main-menu">
            <li class="ripple-btn">
                <ReactLink to="/web-class.gr/" onClick={this.SetLessonDetails.bind(this)}>
                  <span class="item-align-fix">
                    <i class="glyphicon glyphicon-home" style={{'marginRight': '10px'}}></i>
                    <strong>
                      <span>AΡΧΙΚΗ</span>
                    </strong>
                  </span>
                </ReactLink>
              </li>

              {this.main_menu}
          </ul>
        </nav>
      </div>
    );

ReactLink有什么问题吗?在我的apache机器上,所有工作都按预期工作.我无法弄清楚为什么我的程序会中断.

更新

我提供网站的链接,以帮助您的工作变得更容易.该网站在希腊,虽然我相信你可以理解它的结构.

web-class.gr

Code on Github

解决方法

第二次更新:

将react / index.js中的渲染代码更改为

ReactDom.render(
  <Router history={browserHistory} >
    <Route path="/" component={Layout} >
      <IndexRoute component={Index} ></IndexRoute>
      <Route path="/exercise-1" name="exercise-1" component={Exercise1} ></Route>
      <Route path="/exercise-2" name="exercise-2" component={Exercise2} ></Route>
      <Route path="/exercise-3" name="exercise-3" component={Exercise3} ></Route>
      <Route path="/exercise-4" name="exercise-4" component={Exercise4} ></Route>
      <Route path="/exercise-5" name="exercise-5" component={Exercise5} ></Route>
      <Route path="/exercise-6" name="exercise-6" component={Exercise6} ></Route>
      <Route path="/:lessonName" name="lesson" component={Lesson} ></Route>
      <Route path=":lessonName" name="lesson" component={Lesson} ></Route>
    </Route>
  </Router>,app);

我……路径以/开头

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读