【发布时间】:2019-01-09 16:21:56
【问题描述】:
下面是来自 App.js 的代码 sn-p,其中包含 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
我已尝试针对此问题列出的多种方法,但没有解决我的错误。任何人都可以帮助我吗?
【问题讨论】:
-
如果您正确缩进代码,您可能会获得更多帮助。这很难读。
-
除了从props中破坏位置,你的代码中没有使用它吗?那你为什么要破坏它?
-
错误是说 yout
props没有定义。调试看看你在道具上得到了什么 -
我尝试使用少数人建议的解决方案,但没有帮助。
-
即使没有破坏道具,我也遇到了同样的错误
标签: javascript reactjs react-router react-router-v4 react-router-dom