2 回答
![?](http://img1.sycdn.imooc.com/545850a00001fdd002200220-100-100.jpg)
TA贡献1780条经验 获得超4个赞
在 React-Router 设置中导航的方法是使用Linkrepo 提供的组件。您为AddItem组件创建附加路由的第一个建议是正确的。只需导入Link组件并定义要转到的预期路径。
import { Link } from "react-router-dom
class InventorySystem extends React.Component{
constructor(props){
super(props);
this.state = {}
}
render(){
return(
<Link to="/addItem">Add Item</Link>
)
}
}
Link如果需要,您可以将 设置为看起来像按钮,因为它确实接受 className 属性。
![?](http://img1.sycdn.imooc.com/545864000001644402200220-100-100.jpg)
TA贡献1831条经验 获得超4个赞
您有 2 个选项,均包含在下面的示例中
import { Link } from "react-router-dom";
class InventorySystem extends React.Component {
constructor(props) {
super(props);
this.state = {}
}
goTo(e) {
// option 1
this.props.history.push('/inventory/additem');
}
render() {
return (
<div>
<button onClick={this.goTo.bind(this)}>Add Item</button> // option 1
<Link to="/inventory/additem">Add Item</Link> // option 2
</div>
)
}
}
添加回答
举报