当前位置: 首页 > 知识库问答 >
问题:

组件未使用React中的路由器链接进行渲染。js

诸龙野
2023-03-14

来自导航的代码片段。js

类导航扩展组件{render(){return(

    <Router>
 <nav className="navbar navbar-expand-lg navbar-light bg-light">
          <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span className="navbar-toggler-icon"></span>
    </button>

    <div className="collapse navbar-collapse" id="navbarSupportedContent">
      <ul className="navbar-nav mr-auto">

          <NavDropdown name="Test">
            <a className="dropdown-item" >

                <li><Link to={"/Invoice"} >Mexico Invoice</Link></li>

                   </a>
          </NavDropdown>

           <NavDropdown name="Analysis">
            <a className="dropdown-item" href="/Transaction">Audit Log</a>
        </NavDropdown>
      </ul>

   <Route exact path="/Invoice" component={Invoice}  />
             </div>
           </nav>
            </Router>
);

###

使用路由器导出默认值(导航)

我渲染这个组件从App.js

class App extends Component {
  constructor(props) {
    super(props);

  }

  render() {
    return (
      <Router>
      <div >
        <HeaderAmex className="App" />
        <div>
   <Navigation/>
   </div>
   <Route exact path="/invoice" component={Invoice} /> 
         </div>
         </Router>
    );
  }
}

使用路由器导出默认值(应用程序

下面是我的发票。js

class Invoice extends Component {
  constructor(props) {
    super(props);
    this.state = { items: [], isLoaded: false, transId: "" ,flag:true,errorFlag:false,show:false,displayContent:"",invoice:"",invoiceXmlBody:"",invoiceTransId:"",invoiceResultFlag:false,invoicedisplayFlag:false};

  }



  handleChangeInvoice(e) {
    console.log("inside handleChangeInvoice");
    let invoiceXml = e.target.value;
    if (invoiceXml !== undefined) {
      this.setState({ invoiceXmlBody: e.target.value });
    }
  }

  handleSubmitInvoiceXml =e=>{
   console.log("*******************inside handleSubmitInvoiceXml***************");
   let url = "xxxxxx";
   fetch(url, {
    method: "POST",
    body: JSON.stringify(this.state.invoiceXmlBody),
    headers: {
      "Content-Type": "application/xml"
    },
    credentials: "xxxxx"
  }).then(res => res.json())
  .then(json => {
    this.setState({
      invoiceTransId:json,

    });
    if(json===undefined){
this.state.invoiceResultFlag=true;
    }
    else{

      this.state.invoicedisplayFlag=true;
      console.log("inside=========else===="+this.state.invoicedisplayFlag);
    }
    }
 ) }


  render() {
    const { match, location, history } = this.props
    console.log("---------------------------"+this.state.invoicedisplayFlag);
    return (

             <div className="App">
                <div>{location.pathname}</div>
        <br/>

          <label className="lable" style={{marginRight:19}}>
              InvoiceXml:
              <input
                type="text"
                name="invoiceXml" placeholder="Enter invoice xml" 
                onBlur={this.handleChangeInvoice.bind(this)}  style={{marginLeft:15}}
              />

            </label><br/><br/>


             <input type="button" onClick={this.handleSubmitInvoiceXml} name="Submit" value="Submit" className="submitButton"  style={{marginLeft:-60}}/>


            <br/>
            <br/><br/>



            <div  className={this.state.invoicedisplayFlag?"showDisplay":"hide"}>
            <h5>Transaction Id is :{this.state.invoiceTransId}</h5>



        </div>


      </div>
    );
  }
}

export default withRouter(Invoice)

下面是我的index.js

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

 serviceWorker.unregister();

有人能帮我显示发票组件吗。我还没有反应过来。

共有2个答案

慕朝明
2023-03-14
import { BrowserRouter as Router, Route } from 'react-router-dom';

<Router>
   <div>
     <Route exact path="/" component={Home}/>
     <Route path="/news" component={NewsFeed}/>
   </div>
</Router>

这是react路由器dom的示例代码。使用链接组件,该组件使用您之前应该定义的路由。我认为您也缺少一些设置。你应该将你的应用程序包装在路由器中。

我希望这对你有帮助。但你似乎不明白我的意思。阅读这些文件会对你有所帮助。

慕铭
2023-03-14

你得到这个错误,因为导航组件在主路由器之外,组件路径应该是路径={''}而不是路径={''}

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

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

 render() {
   return (
   <Router>
    <div >
        <HeaderAmex className="App" />
        <div>
           <Navigation/>
        </div>

        <Route exact path="/" render={()=><h1>main<h1 />} /> 

        <Route path="/invoice" component={Invoice} /> 
    </div>
   </Router>    
   );
 }
 }

export default App;

为了访问组件中的位置道具,您必须使用路由器(发票)

如何将React路由器位置道具传递给组件?

 类似资料:
  • 问题: 有异步路由,从中获取参数并返回(组件)。问题是当我从一个到另一个(只是相同的路由,但不同。参数)我的路由器卸载并重新渲染组件。但它应该只传递新的道具,不要触及我的组件生命周期。怎么解决呢?我做错了什么? 代码: 路由器 组件将装入电影组件中 更新:好的,做了一些实验。 替换

  • 我试图渲染父组件子组件与默认渲染组件通过相同的路由。 例如 父母组件可以通过此路由访问/破折号/概述现在在父母组件中,我想呈现一个与父母组件具有相同路由的默认组件。 ParentComponent.js 我想渲染。。它工作正常,但当我将路径更改为时,它不会呈现 我正在使用路由器在路由之间切换,我也在使用

  • 我是一个新的反应和反应。我已经花了几个小时了,我只是走到了死胡同。 我正在使用CreateReact应用程序和用于css的bulma。我的依赖项: }, 我查看了stackoverflow的所有相关问题,创建了react应用程序文档,并在他们的回购协议中搜索了他们的开放问题部分,谷歌搜索了所有可能的关键字。我不知所措。 我的路由器只会呈现一个路由(Homeroute)。 如何让它显示/创建路线?

  • 我使用开关标签路由不同的路由 但这只会加载路径/和/主页的第一个组件,也会加载loginpage nothing,并且主页组件不会加载到/主页路径! 我在切换前尝试过使用浏览器路由器标签,尝试过先加载主页,也是一样的只有主页组件加载 App.js 登录age.js 家age.js 预期 访问/应该加载登录页面 访问/主页应加载主页

  • 我正在使用React和React路由器尝试将我的组件链接到一个项目中。我想从主页(当前组件)链接一张图片到另一个组件。 目前我可以点击图片,它有点像一个链接(有时点击后变成蓝色),尽管它没有链接到其他组件,没有错误显示,网址栏中没有任何变化。什么都没发生。 有什么想法吗?这是我的代码: 代码错误:react dom。发展js:17117上述错误发生在组件中:img(由Home创建)中div(由Ho

  • I my app.js我已将路线定义为: 在我的中,我有一个链接: 但问题是,当我在aboue path中设置prop'确切={true}时,找不到这个路径 当我没有在上面的路径中设置exact={true}时,在同一页面上呈现路径组件及其下方的路径组件