- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
想知道是否有一种方法可以根据每个选项卡的嵌套路由呈现路由。
例如,当我们浏览到
http://localhost:3000/base/a
http://localhost:3000/base/b
我们希望有一个 base
组件,它提供某种布局,并根据我们点击的 tab
渲染 nested route
组件.
下面的示例在不使用 react-router 渲染选项卡组件的情况下工作。
下面是路由配置
<Route path={"/base:scope" } exact={true} { component: BaseComponent } />
下面的组件将呈现带有标签的页面
import { Layout, Tabs, Row, Col } from 'antd';
import { Link, NavLink, Route, Switch } from 'react-router-dom';
export class BaseComponent extends React.Component {
public state = {
collapsed: false,
};
private onCollapse = collapsed => {
this.setState({ collapsed });
}
get tabs() {
const { collaborators, integration, settings, configuration, billing, tokens, referrals } = this.props;
return [
{
key: 'a',
tab: 'a',
component: ComponentA,
path: '/base/a',
},
{
key: 'b',
tab: 'b',
component: ComponentB,
path: '/base/b',
},
];
}
public componentWillReceiveProps(props) {
if (!_.isEqual(_.get(this.props, 'match.url'), _.get(props, 'match.url'))) {
this.setState({ key: _.get(props, 'match.url') });
}
}
public render() {
const { renderer, router } = this.context;
const { onLogOut, match, user, profile } = this.props;
return (
<React.Fragment>
<Header className={renderer.renderRule(styles.header, this.props)}>
<div className={renderer.renderRule(styles.title, this.props)}>
Account Settings
</div>
</Header>
<Content>
<div className={renderer.renderRule(styles.container, this.props)}>
<Tabs
animated={false}
defaultActiveKey={match.url}
onChange={key => router.history.push(key)}
className={`${renderer.renderRule(styles.tabs)}`}
>
{_.map(this.tabs, (record, index) => (
<TabPane
key={record.path}
className={renderer.renderRule(styles.pane)}
tab={<span>{record.tab}</span>}
>
{React.createElement(record.component, null, null)}
</TabPane>
))}
</Tabs>
</div>
</Content>
</React.Fragment >
);
}
}
期望:
我们想写得更像 react-router 一样
<Routes path={"/base"} exact={false} component={ComponentBase} />
<Routes path={"/base/a"} exact={true} component={ComponentBase} />
<Routes path={"/base/b"} exact={true} component={ComponentBase} />
但在这种情况下,我们不知道如何渲染页面,因为 react 路由器不渲染页面,所以我们注意到选项卡但没有渲染内容。
这是修改后的组件,没有渲染内容,因为我们希望 react-router 渲染组件。
export class BaseComponent extends React.Component {
public state = {
collapsed: false,
};
private onCollapse = collapsed => {
this.setState({ collapsed });
}
get tabs() {
const { collaborators, integration, settings, configuration, billing, tokens, referrals } = this.props;
return [
{
key: 'a',
tab: 'a',
path: '/base/a',
},
{
key: 'b',
tab: 'b',
path: '/base/b',
},
];
}
public componentWillReceiveProps(props) {
if (!_.isEqual(_.get(this.props, 'match.url'), _.get(props, 'match.url'))) {
this.setState({ key: _.get(props, 'match.url') });
}
}
public render() {
const { renderer, router } = this.context;
const { onLogOut, match, user, profile } = this.props;
return (
<React.Fragment>
<Header className={renderer.renderRule(styles.header, this.props)}>
<div className={renderer.renderRule(styles.title, this.props)}>
Account Settings
</div>
</Header>
<Content>
<div className={renderer.renderRule(styles.container, this.props)}>
<Tabs
animated={false}
defaultActiveKey={match.url}
onChange={key => router.history.push(key)}
className={`${renderer.renderRule(styles.tabs)}`}
>
{_.map(this.tabs, (record, index) => (
<TabPane
key={record.path}
className={renderer.renderRule(styles.pane)}
tab={<span>{record.tab}</span>}
>
</TabPane>
))}
</Tabs>
</div>
</Content>
</React.Fragment >
);
}
}
最佳答案
我尝试使用 antd Tabs 创建 RoutedTabs 组件。我为此添加了文档字符串。
import React from "react";
import PropTypes from "prop-types";
import { Tabs } from "antd";
import { Switch, Route } from "react-router-dom";
import _each from "lodash/each";
import _map from "lodash/map";
const { TabPane } = Tabs;
/**
* RoutedTabs is Ant Design based dynamic navigation tabs component.
* It takes router config as prop and render the component as well as update the window location corrosponding to the tab.
* Example of routeConfig
* overview: {
label: "Overview",
component: RestaurantOverview,
getRoute: url => url
},
menu: {
label: "Menu",
component: Menu,
getRoute: url => `${url}/menu`
},
"menu-holiday-slot": {
label: "Menu Holiday Slot",
component: MenuHolidaySlots,
getRoute: url => `${url}/menu-holiday-slot`
}
This will render three tabs Overview, Menu, Menu Holiday Slot and routes based on what getRoute method returns.
*/
const RoutedTabs = props => {
const { tabsProps, routeConfig } = props;
const { url, path } = props.match;
const tabToRouteMap = {};
const routeToTabsMap = {};
_each(routeConfig, (configObj, routeKey) => {
const routeURL = configObj.getRoute(url);
tabToRouteMap[routeKey] = routeURL;
routeToTabsMap[routeURL] = routeKey;
});
const defaultActiveKey = routeToTabsMap[props.history.location.pathname];
const tabPaneNodes = _map(routeConfig, (configObj, routeKey) => (
<TabPane tab={configObj.label} key={routeKey} />
));
const routeNodes = _map(routeConfig, (configObj, routeKey) => (
<Route
path={configObj.getRoute(path)}
exact
key={routeKey}
component={configObj.component}
/>
));
const onTabChange = activeKey => {
props.history.push(tabToRouteMap[activeKey]);
};
return (
<>
<Tabs {...tabsProps} onChange={onTabChange} defaultActiveKey={defaultActiveKey}>
{tabPaneNodes}
</Tabs>
<Switch>{routeNodes}</Switch>
</>
);
};
RoutedTabs.propTypes = {
tabsProps: PropTypes.object.isRequired, // As per https://ant.design/components/tabs/#Tabs
routeConfig: PropTypes.object.isRequired,
match: PropTypes.object.isRequired,
history: PropTypes.object.isRequired
};
export default RoutedTabs;
关于reactjs - antd tabs 使用 react-router,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50729364/
我有一个已发布的库,其中包含一个在其模板中使用 [routerLink] 的组件。在我的应用程序中安装库后,我收到错误 NullInjectorError:R3InjectorError(AppMod
/~/@ReMix-Run/Router/dist/router.cjs.js模块分析失败:/HOME/sharib/Desktop/Full Stack Developer Tutorial/MER
在 vuejs 2 中,我能够通过返回渲染 html 标签直接在路由器中渲染。我正在尝试使用 vue-router 4 在 vue3 中做同样的事情,但它似乎不起作用: { path:
在我的应用程序中,我使用 vue-router。当我使用 router.map({}) 将路由传递给路由器时,路由可以工作,但当我在构造函数中传递它们时,路由就不起作用。知道这是为什么吗? // wo
我正在 react 应用程序中实现路由。我对react-router 4.0相对于react-router-redux的优势感到困惑。我也对react-router-dom感到困惑。 react-ro
我有以下两条路线:/items和/items/buy。 每个路线在一个 View 中对应一个选项卡。两条路线均使用精确的 Prop 进行渲染,但导航至/items/buy时,仍将两个选项卡标记为事件选
我有一个带有 url/product/0 的新产品页面。当使用按钮保存产品时,我想显示“已成功保存”消息并将 url 更改为/product/1024 而无需实际导航到该页面,因为这会导致对页面上的产
我对此有些困惑。我定义了一条名为classes/:id的路线。在应用程序中导航到该路线时,将调用componentDidMount()。但是,当重新加载页面或复制并粘贴URL时,页面将完全加载,但根本
假设我有以下2条路线: ... ... 现在,当我尝试按路线/news时,会触发带有Home参数的param1的根路线... 我认为解决方案是在像/?param1这
我正在将 Vue Router 与 Vue 3 一起使用,并尝试添加一个包罗万象的路由以在用户尝试访问无效 URL 时重定向用户。当我尝试使用通配符 (*) 时,我将以下错误记录到控制台: Uncau
我在我的项目中使用react-router-dom 4.0.0-beta.6。我有如下代码: 我想在 HomePage 组件中获取查询参数。我找到了 location.search 参数,它看起来像
我正在使用 Vue 路由器。在我的代码中,我曾经有: 这有效,并且页面数据被传递到组件。但是如何使用路由器 View 执行相同的操作呢?这似乎不起作用: js: var vm = new Vue(
我目前正在一个项目中使用react-router-redux,除了我不知道如何访问react-router路由器之外,一切都工作正常。 @connect((state, props) => {
当使用链接时,路由器按预期工作,尽管我收到警告 [history] pushState is deprecated;使用推送代替。 使用react-router-redux中的routeActio
我想创建一个多页 Meteor 网络和移动应用程序。因此,我正在寻找路由服务。 来自 research ,我收集了Iron Router就是我要找的。但是很多教程也提到了Meteor.Router I
我正在使用 React Router(版本 4.0.0)和 Router组件有一个名为“history”的必需 Prop 。 我知道在以前版本的react-router中你可以导入browserHis
我正在使用 氟罗包用于导航。 Fluro 和 Flutter 正在使用“路由器”。所以他们的类(class)合并了。我怎么能解决这个问题? lib/routers/routers.dart:2:1:
自从我更新了 Vue.js 应用程序的节点包后,我在浏览器控制台中收到以下警告: [vue-router] 's tag prop is deprecated and has beenremoved
我正在尝试在我的应用程序中将 react-router 更新到 v2.6 并将 react-router-relay 更新到 v0.7,但我正在努力按照变更日志来解决所有重大变更。我想我解决了所有的变
router-view 和 router-link 组件无法在我的应用中解析不知道是什么问题 附上应用代码它是一个主页应用程序,带有类似 navbar 的东西,用于路由到 vue 组件,如 state
我是一名优秀的程序员,十分优秀!