简体   繁体   English

reactjs路由器-设置登录页面

[英]reactjs router - setup a login page

I'm trying to make a login page that would work as entry page for my app, and after user would input login and password, it would open main component with menu and content of the pages. 我正在尝试制作一个登录页面,该页面可用作我的应用程序的输入页面,并且在用户输入登录名和密码后,它将打开包含菜单和页面内容的主要组件。

the problem that I can'r understand how to setup a router, or main app structure so it would work as I want. 我无法理解如何设置路由器或主应用程序结构的问题,这样它就可以按我的意愿工作。 So far I can display a login component with main menu bar at the same time, or just a login page without working pages. 到目前为止,我可以同时显示带有主菜单栏的登录组件,或者仅显示没有工作页面的登录页面。

there is my router 有我的路由器

import React from 'react'
import ReactDOM from 'react-dom' 
import {Router, Route, IndexRoute, hashHistory} from 'react-router'
import App from './App'
import LoginPage from './pages/login'
import Forms from './pages/Forms'
import Home from './pages/Home'

ReactDOM.render(
  <Router history={hashHistory}>
    <Route path ="/" component={App}>
      <IndexRoute component={LoginPage} />
      <Route exact path='/LoginPage' component={LoginPage} />
      <Route exact path='/App' component={App} />
      <Route exact path='/Home' component={Home} />
      <Route exact path='/Forms' component={Forms} />
    </Route>   
  </Router>, document.getElementById('root'))

my login page 我的登录页面

import React, { Component } from 'react'
import { Link } from "react-router"
import logo from './logo.jpg';

export default class LoginPage extends Component {
  render() {
    return (
        <div className="login-page">
            <div className="login-form-cont">
                <div style={{paddingBottom:'10px'}}> <img src={logo} width="270" height="54" /> </div>
                <form className="login-form">
                    <input type="text" placeholder="username"/>
                    <input type="password" placeholder="password"/>
                    <Link to={`/Home`}> 
                        <button> login </button>  
                    </Link>
                </form>
            </div>
        </div> 
    )
  }
}

and my app.js 和我的app.js

import React, { Component } from 'react'
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider'
import getMuiTheme from 'material-ui/styles/getMuiTheme'
import classnames from 'classnames'
import SideBarMenu from './components/SidebarMenu'

export default class App extends Component {
    constructor(props){
    super(props)
    this.state = {open: false}
  }

  render() {
  return (

      <MuiThemeProvider muiTheme={muiTheme}>
      <div >
          <SideBarMenu/>
          <div className={classnames('app-content')}>
              { this.props.children }
          </div>
      </div>
      </MuiThemeProvider>
      )
  }
}

I tried to make router to start from login component but in this case even address is changing in a browser window, there was no changes on the screen itself 我试图使路由器从登录组件开始,但是在这种情况下,即使浏览器窗口中的地址发生了变化,屏幕本身也没有变化

I'm using react router 3.2 and react 15.6 我正在使用React Router 3.2和React 15.6

first of all i will recommend you use react-router v4 cos that what i will be showing you. 首先,我将建议您使用react-router v4 cos,我将向您显示。 and v4 is beta than the older versions. 并且v4是较旧版本的beta版本。

import { BrowserRouter, Switch, Route } from 'react-router-dom';

export default class App extends Component{
  render(){
    return (
     <BrowserRouter>
        <Switch>
           <Route exact path="/" component={Login}/>
           <Route exact path="/Home" component={Home}/>
           <Route exact path="/Dashboard" component={Dashboard} />
           <Route exact path="/Dashboard/:userId" component={Dashboard}/>
           <Route component={NotFound}/>
        </Switch>
      </BrowserRouter>
    )
  }
}

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

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