gpt4 book ai didi

node.js - 更改组件内的路由会更改整个页面

转载 作者:太空宇宙 更新时间:2023-11-04 01:27:21 26 4
gpt4 key购买 nike

我有一个应用程序,当用户登录时,他/她会被带到仪表板,其中有一个侧边栏,其中有指向各种组件的链接,例如(主页、搜索、我的帐户),并且我已在该组件中包含了 BrowserRouter 并定义了路由和相应的组件,但是当我单击其中一个链接时,整个页面消失并且没有显示任何内容,但是,当第一次加载页面时,主页组件会正确加载,之后如果我单击任何链接,则任何内容都无法正常工作

import React, { Component } from "react";
import { Layout, Menu, Icon, Modal } from "antd";
import jwtDecode from "jwt-decode";
import {
withRouter,
Link,
Switch,
BrowserRouter,
Route
} from "react-router-dom";
import Home from "./components/Home";
import Search from "./components/Search";
import MyAccount from "./components/MyAccount";

const { Header, Sider, Content } = Layout;

class Dashboard extends Component {
state = {
collapsed: false,
modalVisible: false
};

toggle = () => {
this.setState({
collapsed: !this.state.collapsed,
current: 1
});
};

toggleModal = () => {
this.setState({
modalVisible: true
});
};

handleOk = () => {
localStorage.removeItem("user");
this.setState(
{
modalVisible: false
},
() => {
this.props.history.push("/");
}
);
};

handleCancel = () => {
this.setState(
{
modalVisible: false,
current: 1
},
() => {
console.log(this.state);
}
);
};

componentDidMount() {
const token = localStorage.getItem("user");

if (token) {
const decoded = jwtDecode(token);

if (Date.now() < decoded.exp) {
console.log("logged out");
this.props.history.push("/");
} else {
console.log("logged in");
this.props.history.push("/dashboard");
}
} else {
this.props.history.push("/");
}
}

handleClick = e => {
this.setState({
current: e.key
});
};

render() {
return (
<Layout>
<Sider
trigger={null}
collapsible
collapsed={this.state.collapsed}
style={{ minHeight: "100vh" }}
>
<Menu
theme="dark"
mode="inline"
defaultSelectedKeys={["1"]}
style={{ marginTop: "5rem" }}
>
<Menu.Item key="1">
<Link to="dashboard/home" exact="true">
<Icon type="home" />
<span>Home</span>
</Link>
</Menu.Item>
<Menu.Item key="2">
<Link to="dashboard/search" exact="true">
<Icon type="search" />
<span>Search</span>
</Link>
</Menu.Item>
<Menu.Item key="3">
<Link to="dashboard/my-account" exact="true">
<Icon type="user" />
<span>My account</span>
</Link>
</Menu.Item>
<Menu.Item key="4" onClick={this.toggleModal}>
<Icon type="logout" />
<span>Log out</span>
</Menu.Item>
</Menu>
</Sider>
<Layout>
<Header style={{ background: "#fff", paddingLeft: "20px" }}>
<Icon
className="trigger"
type={this.state.collapsed ? "menu-unfold" : "menu-fold"}
onClick={this.toggle}
/>
</Header>
<Content
style={{
padding: 24,
background: "#fff",
minHeight: 280
}}
>
<Modal
title="Log out"
visible={this.state.modalVisible}
onOk={this.handleOk}
onCancel={this.handleCancel}
>
<p>Are you sure you want to log out?</p>
</Modal>
<BrowserRouter>
<Switch>
<Route to="/home" component={Home} exact />
<Route to="/search" component={Search} exact />
<Route to="/my-account" component={MyAccount} exact />
</Switch>
</BrowserRouter>
</Content>
</Layout>
</Layout>
);
}
}

export default withRouter(Dashboard);

最佳答案

您的代码中存在多个问题。

Route你写了to ,

<Route to="/home" component={Home} exact />

没有toRoute你应该使用path

<Route path="/home" component={Home} exact />

另一个问题是,

<Link to="dashboard/my-account" exact="true">

您应该以 / 开始您的路径,并且不要写 exact这里

<Link to="/dashboard/my-account">

还是上面的Link不会工作,因为你没有 Route来处理这个问题。

要实现此功能,您应该使用 Link是这样的,

<Link to="/home">
<Link to="/search">
<Link to="/my-account">

另一个问题是,

this.props.history.push("/");

这也不起作用,因为你没有 Route对于路径/ .

要实现此功能,您应该具有 Route就像,

<Route path="/" component={Home} exact />

引用thisthis了解有关路由的更多信息。

<小时/>

更新

App.js将您的路线更改为此,

<BrowserRouter basename="dashboard"> //for basename refer second link above
<Switch>
<Route path="/" component={Dashboard} />
</Switch>
</BrowserRouter>

Dashboard.js ,删除 <BrowserRouter>并且只有 switch就像,

<Switch>
<Route path="/home" component={Home} exact />
<Route path="/search" component={Search} exact />
<Route path="/my-account" component={MyAccount} exact />
</Switch>

你的链接应该是,

<Link to="/home">
<Link to="/search">
<Link to="/my-account">

关于node.js - 更改组件内的路由会更改整个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57052820/

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