这不是一个特定的问题,但我想为诸如 localhost:3001/users/jim127 之类的配置文件生成自定义 URL。有人知道这样做的好方法吗?
您可以使用 React Router https://github.com/ReactTraining/react-router为您的应用程序创建一个简单的路由。
在您的情况下,您需要为指定用户创建动态路由。
<Router history={browserHistory}>
<Route path="/" component={App}>
<Route path="users" component={Users}>
<Route path="/users/:userName" component={User}/> // dynamic route
</Route>
<Route path="*" component={NoMatch}/>
</Route>
</Router>
Run Code Online (Sandbox Code Playgroud)
下一步是userName从User组件中的 URL 中获取
class User extends React.Component {
componentDidMount: function () {
const userName = this.props.params.userName
...
Run Code Online (Sandbox Code Playgroud)
并简单地加载指定的任何数据 userName
| 归档时间: |
|
| 查看次数: |
1890 次 |
| 最近记录: |