错误为:TypeError:无法读取未定义的属性“位置”

时间:2019-01-09 16:21:57

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

下面是来自App.js的代码段,其中包括Navigator组件:

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>
    );
  }
}

export default withRouter(App)

我的导航组件如下所示。它获取位置作为属性:

class Navigation extends Component {
  render() {
    const { match, location, history } = this.props

    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>
    );
  }
}

export default withRouter(Navigation)

下面是Invoice.js中的代码

render() {
  const { match, location, history } = this.props
  console.log("---------------------------"+this.state.invoicedisplayFlag);
  return (
    <div className="App">
      <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/>
      <input
        type="button"
        onClick={this.handleSubmitInvoiceXml}
        name="Submit"
        value="Submit"
        className="submitButton"
        style={{marginLeft:-60}}
      />
      <br/>
      <div className={this.state.invoicedisplayFlag ? "showDisplay" : "hide"}>
        <h5>Transaction Id is :{this.state.invoiceTransId}</h5>
      </div>
    </div>
  );
}

我遇到了错误:

"TypeError: Cannot read property 'location' of undefined" at Router.js:66

我已尝试针对此问题列出多种方法,但没有任何方法解决我的错误。有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

欢迎来到stackoverflow,

我不确定我是否正确理解了您的问题,因为您大多在显示代码,但没有具体说明发生错误的情况或位置。

我看到的是,您正在通过Invoice属性将component组件传递给路由。读完this github post后,看来在react-router v4中,您应该使用render属性将道具和您的组件一起移交。

所以基本上您应该做的是:

  1. 创建一个将props作为参数并返回具有所需props的Invoice组件的函数

  2. 在Navigator.js中将<Route exact path="/Invoice" component={Invoice} />更改为<Route exact path="/Invoice" render={MyInvoiceFunction} />

  3. 请记住,为您的下一个问题提供更多信息和格式正确的代码;)

希望这可以解决您的问题

编辑:

您应该将该函数添加到Invoice.js中,例如:

export const MyInvoiceFunction = (props) => {
  return (
    <Invoice match={this.match.bind(this)} location={this.location.bind(this)} {...props} />
  );
};