是的,您可以通过在 URL 中手动输入路径来导航到任何定义的路径。例如:在下面的代码中,我创建了 Dashboard 和 information 两个组件,并使用 react-router-dom 在我的应用程序中提供路由功能。使用以下代码,您可以导航到特定组件。如果您输入http://server:port/dashboard,它将导航到仪表板组件,如果您输入http://server:port/information,它将导航到信息组件
应用程序.js 文件
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
import React from 'react';
import Dashboard from './Dashboard.js';
import Information from './Information.js';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {open: false};
this.state = {message: "StackOverflow"};
}
render(){
return (
<Router>
<div>
<Route exact path="/dashboard" render={props => <Dashboard {...props} />} />
<Route exact path="/information" render={props => <Information {...props} />} />
</div>
</Router>
);
}
}
仪表板.js
import React from 'react';
class Dashboard extends React.Component {
render() {
return (
<div ><h1> Hello React</h1></div>
);
}
}
export default Dashboard;
信息.js
import React from 'react';
class Information extends React.Component {
render() {
return (
<div ><h1> Hello React-Router-DOM</h1></div>
);
}
}
export default Information;
我希望它会帮助你。