React Router无法正常链接到标记

时间:2017-08-10 06:21:08

标签: reactjs react-router react-router-v4 react-router-dom react-router-component

我有三个js文件。父母我要打电话给孩子班。代码是这样的。

这是父类App.js

import React from 'react';
import ReactDOM from 'react-dom';
import $ from 'jquery';
import {FirstPage} from './FirstPage.js';
import {Panorama} from './Panorama.js';
import {BrowserRouter,Route,Router,Switch} from 'react-router-dom';

class App extends React.Component{
    constructor(props){
      super(props);
    }

    render(){
      return(
        <div>
          <BrowserRouter>
            <Switch>
              <Route exact path="/" component={FirstPage} />
              <Route path=":id" component={Panorama} />
            </Switch>
          </BrowserRouter>
        </div>
        )
    }
  }

ReactDOM.render(<App/>,document.getElementById('container'));

FirstPage.js是这样的

import React from 'react';
import ReactDom from 'react-dom' ;
import $ from 'jquery' ;
import {Panorama} from './Panorama.js';
import {Redirect,Link} from 'react-router-dom';
import {withRouter} from 'react-router';

class FirstPage extends React.Component{
    constructor(props){
      super(props);
      this.state={
        list:[],
        images:[],
        isClicked:false,
        redirect:true,
        imageUrl:''
      }
      this.loadImages=this.loadImages.bind(this);
      this.loadOne=this.loadOne.bind(this);
    }
    componentDidMount(){
        window.addEventListener('load',this.loadImages);
   }

   loadImages(){ 
      console.log("load");
      var that=this;
      $.ajax({
        type:'GET',
        url:'https://demo0813639.mockable.io/getPanos',
        datatype:'jsonp',
        success:function(result){
          var images=that.state.images;
          for(var i=0;i<result.length;i++){
            that.state.images.push({"pano":result[i].pano,"name":result[i].name});
          }
          that.setState({
            images:images
         })
        }

      })
   }

   loadOne(pano){
    console.log("pano: ",pano);
    this.setState({
      isClicked:true,
      imageUrl:pano
    })
    //this.props.history.push(`/image/${pano}`)
  }

  render(){
    var list=this.state.list;
    console.log("Image URL: "+this.state.imageUrl);
    return this.state.isClicked?<Link to={`${this.state.imageUrl}`}/>:
        <div> {this.state.images.map((result)=>{
        return(<div className="box">
                <div className="label">{result.name}</div>
                  <img src={result.pano} className="image col-md-3" onClick={this.loadOne.bind(this,result.pano)}/>   
              </div>
              )

       })}
       </div>
  }
}

module.exports={
  FirstPage:FirstPage
}

此页面进行ajax调用并在屏幕上加载四个图像。如果点击其中一个图像,则应该使用该图像的id调用另一个js文件,以便可以全屏显示特定图像

import React from 'react';
import ReactDOM from 'react-dom';
import $ from 'jquery';
import {Link} from 'react-router-dom';

class Panorama extends React.Component{
    render(){
      console.log("Image: "+ props.match.params.id);
      return( 
        <div>
          <img src={`${props.match.params.id}`}/>
          </div>
        )
    }
  }

module.exports={
  Panorama:Panorama
}

虽然我在控制台中没有出现任何错误,但在点击图像后屏幕上没有任何内容。代码有什么问题?

React路由器的版本为 v4

2 个答案:

答案 0 :(得分:1)

您需要做的第一件事就是@Fawaz建议,更新路线

<Route path="/panorama/:imageUrl" component={Panorama} />
  

当您将url作为参数发送时,您需要在加载图像之前对网址进行编码,并且您可以使用历史记录API更改路径,在某些状态更改时无需使用渲染方法中的链接。我认为这种做法是错误的。我已经更新了loadOne方法,如下所示:

  loadOne(pano){
   let imageUrl = encodeURIComponent(pano);
   this.props.history.push(`/panorama/${imageUrl}`);
  }
  

在Panorama Component中,您可以解码网址并加载图片。

  render() {
     let url = decodeURIComponent(this.props.match.params.id);
     return( 
       <div>
         <img src={`${url}`}/>
       </div>
     )
  }
  

FirstPage.js

import ReactDom from 'react-dom' ;
import $ from 'jquery' ;
import {Panorama} from './Panorama.js';
import {Redirect,Link} from 'react-router-dom';
import {withRouter} from 'react-router';

class FirstPage extends React.Component{
   constructor(props){
     super(props);
     this.state={
       images:[]
     }
  this.loadImages=this.loadImages.bind(this);
}
componentDidMount(){
  window.addEventListener('load',this.loadImages);
}

loadImages(){ 
  var that=this;
  $.ajax({
    type:'GET',
    url:'https://demo0813639.mockable.io/getPanos',
    datatype:'jsonp',
    success:function(result){
      that.setState({
        images:result // I am assuming, result is array of objects.
     })
    }
   })
}

loadOne(pano){
  let imageUrl = encodeURIComponent(pano);
  this.props.history.push(`/panorama/${imageUrl}`);
}

render(){
   return <div> 
           {this.state.images.map((result)=>{
              return(<div className="box">
                <div className="label">{result.name}</div>
                <img src={result.pano} className="image col-md-3" 
                onClick={this.loadOne.bind(this,result.pano)}/>   
                </div>)

           })}
          </div>
  }
}

module.exports={
  FirstPage:FirstPage
}

答案 1 :(得分:0)

您的路径需要与您呼叫的路径相匹配。将您的第二条路线更改为:

<Route path="/image/:id" component={Panorama} />