javascript - 使用反应js_在下一页上没有路由按钮导航到按钮单击时的全新页面
问题描述
我一直在尝试使用Route/BrowserHistory从一个页面导航到另一个页面,但他们所做的是路由器按钮位于页面顶部。但是,我想导航到不包含该路由按钮的完全不同的页面。
例如,在我的情况下,我的第一页由两个按钮Configuration和User Portal组成。单击任何按钮时,我想在单击User Portal时跳转到新页面,即 UserPortal 页面。
这是我的代码:
<div className="App">
<BrowserRouter>
<div>
<nav aria-label="breadcrumb">
<ol className="breadcrumb" style={{background: 'none'}}>
<div className="breadcrumb-item">
<Link to="configuration">
<button className='btn btn-primary' id='config' >Configuration</button>
</Link>
</div>
<div className="breadcrumb-item">
<Link to="userPortal">
<button className='btn btn-primary' id='portal' >User Portal</button>
</Link>
</div>
</ol>
</nav>
<Switch>
<Route path="/configuration" component={ArduinoInterface} />
<Route path="/userPortal" component={UserPortal} />
</Switch>
</div>
</BrowserRouter>
</div>
我应该怎么做才能得到想要的结果?
您的帮助将不胜感激!谢谢。
解决方案
这就是我解决问题的方法。我遵循了这个例子,它奏效了。
索引.js
ReactDOM.render(<Router history={history}>
<Routes />
</Router> , document.getElementById('root'));
路由.js
<Switch>
<Route path="/" exact component={MainPage} />
<Route path="/configuration" component={ArduinoInterface} />
<Route path="/userportal" component={UserPortal} />
<Route component={MainPage} />
</Switch>
MainPage.js
<div>
<button type="button" id='config'><Link to='/configuration'>Configuration</Link></button>
</div>
<div >
<button type="button" id='portal'><Link to='/userportal'>User Portal</Link></button>
</div>
推荐阅读
- ruby-on-rails - Sprocket 错误 - 资产未声明为在生产中预编译
- c - 用于接受和打印字符串的非常小的 c 程序中的分段错误
- javascript - React Native 中的“预渲染”屏幕
- swift - 表达式解析为具有特殊功能的未使用函数
- php - 我需要将此数组中的数据提取到变量中
- c# - 这个 linq OrderBy 方法实际上对任何东西进行排序吗?
- matplotlib - Matplotlib 堆积条形图未显示所有条形
- java - 使用 Bundle 将数据从活动发送到片段
- php - 如何获取两个日期之间的所有日期并插入数据库?
- python - 如何替换txt文件中的一列?