【发布时间】:2019-06-14 13:38:50
【问题描述】:
我正在将简单的员工列表和详细演示放入 ReactJS。我有员工列表组件,它显示从一些虚拟休息网络服务获取的所有员工列表并显示在页面上。
class contactList extends Component {
constructor(props) {
super(props);
this.state = {
employees: [],
};
}
componentDidMount() {
this.EmployeeList();
}
EmployeeList() {
fetch('https://jsonplaceholder.typicode.com/comments')
.then(response => response.json())
.then(results => {
this.setState({ employees: results })
.catch(
error => { console.log(error); }
);
});
}
onDelete(e) {
}
render() {
console.log(this.state.employees);
const persons = this.state.employees.map((item, i) => (
<div>
<table>
<tbody>
<tr>
<td>
<Link to={"/Contactdetails/" + item.id} >{item.name}</Link>
<Route exact path={"/Contactdetails/:id"} component={Contactdetails}></Route>
</td>
<td>
<button onClick={this.onDelete}>
Delete
</button>
</td>
</tr>
</tbody>
</table>
{/* <Link to="/">Delete</Link> */}
</div>
));
return (
<div id="layout-content" className="layout-content-wrapper">
<div className="panel-list">{persons}</div>
</div>
);
}
我已经在页面上显示了所有带有相应 id(parameter) 的标签。单击链接时出现问题,它不会带我进入详细信息页面。在详细页面中,我在渲染方法中也保持警报,但它也没有出现,所以,我认为详细页面没有被调用。
class Contactdetails extends Component {
constructor(props) {
super(props);
this.state = {
};
}
componentWillMount(){
alert("Helloo");
}
componentDidMount() {
console.log("props: ", this.props.match)
this.EmployeeDetails();
}
EmployeeDetails() {
fetch('http://dummy.restapiexample.com/api/v1/employees')
.then(response => response.json())
.then(results => { this.setState({ employees: results }) });
}
render() {
alert("Hello");
return (
<h2>Contact Details</h2>
)
}
};
StackBlitz 链接: https://stackblitz.com/edit/react-f4eha2?file=Contactdetails.js
【问题讨论】:
-
点击链接后的url是什么?
-
/Contactdetails/1 - 像这样的东西。我点击后,它改变了网址,我在页面上看不到任何东西
-
请打开浏览器控制台,可能会出现一些错误
-
了解为什么会这样,请查看我的回答。
-
@UbiquitousDevelopers:试试 Danyal soultion 这就是问题所在
标签: reactjs react-router