-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve routes #351
Improve routes #351
Conversation
Pull Request Test Coverage Report for Build 198
💛 - Coveralls |
Pull Request Test Coverage Report for Build 202
💛 - Coveralls |
更新文档。 umi 会根据 基础路由假设
那么,umi 会自动生成路由配置如下: [
{ path: '/': exact: true, component: './pages/index.js' },
{ path: '/users/': exact: true, component: './pages/users/index.js' },
{ path: '/users/list': exact: true, component: './pages/users/list.js' },
] 动态路由umi 里约定,带 $ 前缀的目录或文件为动态路由。 比如以下目录结构:
会生成路由配置如下: [
{ path: '/': exact: true, component: './pages/index.js' },
{ path: '/users/:id': exact: true, component: './pages/users/$id.js' },
{ path: '/:post/': exact: true, component: './pages/$post/index.js' },
{ path: '/:post/comments': exact: true, component: './pages/$post/comments.js' },
] 可选的动态路由umi 里约定动态路由如果带 $ 后缀,则为可选动态路由。 比如以下结构:
会生成路由配置如下: [
{ path: '/': exact: true, component: './pages/index.js' },
{ path: '/users/:id?': exact: true, component: './pages/users/$id$.js' },
] 嵌套路由umi 里约定目录下有 比如以下目录结构:
会生成路由配置如下: [
{ path: '/users': exact: false, component: './pages/users/_layout.js'
routes: [
{ path: '/users/', exact: true, component: './pages/users/index.js' },
{ path: '/users/:id', exact: true, component: './pages/users/$id.js' },
],
},
] 全局 layoutumi 里约定 比如: export default function(props) {
return (
<>
<Header />
{ props.children }
<Footer />
</>
);
} 不同的全局 layout你可能需要针对不同路由输出不同的全局 layout,umi 不支持这样的配置,但你仍可以在 比如不想要针对 /login 输出简单布局, export default function(props) {
if (props.location.path === '/login') {
return <SimpleLayout>{ props.children }</SimpleLayout>
}
return (
<>
<Header />
{ props.children }
<Footer />
</>
);
} 404 路由umi 中约定 比如: export default () => {
return (
<div>I am a customized 404 page</div>
);
}; 注:开发模式下,umi 会添加一个默认的 404 页面来辅助开发,但你仍然可通过精确地访问 /404 来验证 404 页面。 路由过滤如果你需要在 比如以下目录结构:
你应该只会想要 users/index.js 作为路由,所以需要排除掉 models 和 services 目录。 先安装依赖, $ npm install umi-plugin-routes --save-dev 然后配置 export default {
plugins: [
['umi-plugin-routes', {
exclude: [
/models/,
/services/,
],
}],
]
} 权限路由umi 是通过配置定制化的 Route 组件来实现权限路由的,如果你熟悉 react-router@4,应该会比较熟悉。 比如以下目录结构:
然后在 export default {
pages: {
'/list': { Route: './routes/PrivateRoute.js' },
},
} 则会自动生成以下路由配置: [
{ path: '/': exact: true, component: './pages/index.js' },
{ path: '/list': exact: true, component: './pages/list.js', meta: { Route: './routes/PrivateRoute.js' } },
] 然后 umi 会用
import { Route } from 'react-router-dom';
export default (args) => {
const { render, ...rest } = args;
return <Route
{...rest}
render={props =>
<div>
<div>PrivateRoute (routes/PrivateRoute.js)</div>
{
render(props)
}
</div>
}
/>;
} 注: 配置式路由umi 推荐的路由方式是基于目录和文件的约定的,但如果你倾向于使用配置式的路由,可以在 比如: [
{ "path": "/", "exact": true, "component": "./components/a" },
{ "path": "/list", "component": "./pages/b", "meta": { "Route": "./routes/PrivateRoute.js" } },
{ "path": "/users", "component": "./pages/users/_layout",
"routes": [
{ "path": "/users/detail", "exact": true, "component": "./pages/users/detail" },
{ "path": "/users/:id", "exact": true, "component": "./pages/users/id" }
]
}
] 注意:
路由动效路由动效应该是有多种实现方式,这里举 react-transition-group 的例子。 先安装依赖, $ npm install react-transition-group --save 在 layout 组件( import withRouter from 'umi/withRouter';
import { TransitionGroup, CSSTransition } from "react-transition-group";
export default withRouter(
({ location }) =>
<TransitionGroup>
<CSSTransition key={location.key} classNames="fade" timeout={300}>
{ children }
</CSSTransition>
</TransitionGroup>
) 上面用到的 .fade-enter {
opacity: 0;
z-index: 1;
}
.fade-enter.fade-enter-active {
opacity: 1;
transition: opacity 250ms ease-in;
} 面包屑面包屑也是有多种实现方式,这里举 react-router-breadcrumbs-hoc 的例子。 先安装依赖, $ npm install react-router-breadcrumbs-hoc --save 然后实现一个 import NavLink from 'umi/navlink';
import withBreadcrumbs from 'react-router-breadcrumbs-hoc';
// 更多配置请移步 https://github.com/icd2k3/react-router-breadcrumbs-hoc
const routes = [
{ path: '/', breadcrumb: '首页' },
{ path: '/list', breadcrumb: 'List Page' },
];
export default withBreadcrumbs(routes)(({ breadcrumbs }) => (
<div>
{breadcrumbs.map((breadcrumb, index) => (
<span key={breadcrumb.key}>
<NavLink to={breadcrumb.props.match.url}>
{breadcrumb}
</NavLink>
{(index < breadcrumbs.length - 1) && <i> / </i>}
</span>
))}
</div>
)); 然后在需要的地方引入此 React 组件即可。 启用 Hash 路由umi 默认是用的 Browser History,如果要用 Hash History,需在 export default {
hashHistory: true,
} Scroll to top在 layout 组件( import { Component } from 'react';
import withRouter from 'umi/withRouter';
class Layout extends Component {
componentDidUpdate(prevProps) {
if (this.props.location !== prevProps.location) {
window.scrollTo(0, 0);
}
}
render() {
return this.props.children;
}
}
export default withRouter(Layout); 参考 |
Close #334
/umi.js
访问不到的问题[ ] 修复通过路由跳转到不存在时的路由不会切换到 /404 页面的问题