gpt4 book ai didi

reactjs - React-Router-Dom 母版页

转载 作者:行者123 更新时间:2023-12-03 20:17:29 26 4
gpt4 key购买 nike

我正在尝试使用 react-router-dom 创建母版页.我的 main页面会有nav-bar我只想改变内容。所以在我的站点路由器页面中,我有这样的事情:

import React from 'react'

import { BrowserRouter, Switch, Route } from 'react-router-dom'

import {FundDetails} from "../pages/fund_detail";
import {Dashboard} from "../pages/dashboard";
import {Main} from "../pages/main";


export class SiteRouter extends React.Component {
render(){
return (
<BrowserRouter>
<Switch>
<Route component={Main}/>
<Route path="/fund_details" component={FundDetails} />
<Route path="/dashboard" component={Dashboard} />
</Switch>
</BrowserRouter>
);
}
}

然后在我的 navbar.js我有链接:
import React from 'react';

import { NavItem, Nav, Navbar } from 'react-bootstrap';

import styled from 'styled-components';

import Particles from 'react-particles-js';
import params from './../../components/particlesjs-config.json';
import {PurposeLogo} from "../media/PurposeLogo";


import {
BrowserRouter as Router,
Route,
Link
} from 'react-router-dom'

const StyledNavbar = styled(Navbar)`

background-image: linear-gradient(90deg, #320734 0%, #B71F71 100%);
min-height: 100px;
}
`


export class PurposeNav extends React.Component {
render(){
return(
<StyledNavbar inverse collapseOnSelect fixedTop>
<Particles
height={100}
params={params}
style={{
position: "fixed",
top: 0,
marginLeft: 700,
width: "100%",
height: "100%"
}} />
<Navbar.Header>
<Navbar.Brand>
<PurposeLogo />
</Navbar.Brand>
</Navbar.Header>
<Navbar.Collapse>
<Nav pullRight>
<NavItem><Link to='fund_details'>Fund Details</Link></NavItem>
<NavItem eventKey={2} href="#">Link Right</NavItem>
</Nav>
</Navbar.Collapse>
</StyledNavbar>
);
}
}

所以当我点击 Fund Details ,应该显示图形。但我什么也得不到。有任何想法吗?

最佳答案

<Switch>一旦找到匹配的 <Route> 就会“中断”匹配.没有路径的路由将始终匹配,因此它下面的任何内容都不会匹配。如果你想要 Main始终渲染它根本不需要包装在 Route 中。

  <BrowserRouter>
<div>
<Main />
<Switch>
<Route path="/fund_details" component={FundDetails} />
<Route path="/dashboard" component={Dashboard} />
</Switch>
</div>
</BrowserRouter>

关于reactjs - React-Router-Dom 母版页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47480720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com