From 8d050d404096c863427f6717502e80c3b0c702e0 Mon Sep 17 00:00:00 2001 From: Baorong Li Date: Fri, 20 Mar 2020 11:59:04 +0800 Subject: [PATCH] fix pathToRegexp not work --- src/components/Layout/Bread.js | 2 +- src/components/Layout/Menu.js | 2 +- src/layouts/PrimaryLayout.js | 2 +- src/models/app.js | 2 +- src/pages/dashboard/model.js | 4 ++-- src/pages/login/model.js | 2 +- src/pages/post/model.js | 2 +- src/pages/user/model.js | 2 +- src/utils/index.js | 2 +- src/utils/index.test.js | 8 ++++---- 10 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/components/Layout/Bread.js b/src/components/Layout/Bread.js index 272e4f1..a3a121a 100644 --- a/src/components/Layout/Bread.js +++ b/src/components/Layout/Bread.js @@ -40,7 +40,7 @@ class Bread extends PureComponent { // Find a route that matches the pathname. const currentRoute = routeList.find( - _ => _.route && pathToRegexp(_.route, location.pathname) + _ => _.route && pathToRegexp(_.route).exec(location.pathname) ) // Find the breadcrumb navigation of the current route match and all its ancestors. diff --git a/src/components/Layout/Menu.js b/src/components/Layout/Menu.js index 04d7ebb..3cd91cc 100644 --- a/src/components/Layout/Menu.js +++ b/src/components/Layout/Menu.js @@ -80,7 +80,7 @@ class SiderMenu extends PureComponent { // Find a menu that matches the pathname. const currentMenu = menus.find( - _ => _.route && pathToRegexp(_.route, location.pathname) + _ => _.route && pathToRegexp(_.route).exec(location.pathname) ) // Find the key that should be selected according to the current menu. diff --git a/src/layouts/PrimaryLayout.js b/src/layouts/PrimaryLayout.js index 5000e35..acafce3 100644 --- a/src/layouts/PrimaryLayout.js +++ b/src/layouts/PrimaryLayout.js @@ -70,7 +70,7 @@ class PrimaryLayout extends PureComponent { // Find a route that matches the pathname. const currentRoute = newRouteList.find( - _ => _.route && pathToRegexp(_.route, location.pathname) + _ => _.route && pathToRegexp(_.route).exec(location.pathname) ) // Query whether you have permission to enter this page diff --git a/src/models/app.js b/src/models/app.js index 129d3d1..6afd5c0 100644 --- a/src/models/app.js +++ b/src/models/app.js @@ -13,7 +13,7 @@ import config from 'config' const { queryRouteList, logoutUser, queryUserInfo } = api const goDashboard = () => { - if (pathToRegexp(['/', '/login'], window.location.pathname)) { + if (pathToRegexp(['/', '/login']).exec(window.location.pathname)) { history.push({ pathname: '/dashboard', }) diff --git a/src/pages/dashboard/model.js b/src/pages/dashboard/model.js index 6d0fb07..f448058 100644 --- a/src/pages/dashboard/model.js +++ b/src/pages/dashboard/model.js @@ -35,8 +35,8 @@ export default modelExtend(model, { setup({ dispatch, history }) { history.listen(({ pathname }) => { if ( - pathToRegexp('/dashboard', pathname) || - pathToRegexp('/', pathname) + pathToRegexp('/dashboard').exec(pathname) || + pathToRegexp('/').exec(pathname) ) { dispatch({ type: 'query' }) dispatch({ type: 'queryWeather' }) diff --git a/src/pages/login/model.js b/src/pages/login/model.js index 330ead6..878433a 100644 --- a/src/pages/login/model.js +++ b/src/pages/login/model.js @@ -16,7 +16,7 @@ export default { if (data.success) { const { from } = locationQuery yield put({ type: 'app/query' }) - if (!pathToRegexp('/login', from)) { + if (!pathToRegexp('/login').exec(from)) { if (['', '/'].includes(from)) history.push('/dashboard') else history.push(from) } else { diff --git a/src/pages/post/model.js b/src/pages/post/model.js index dd47deb..6a3ba7d 100644 --- a/src/pages/post/model.js +++ b/src/pages/post/model.js @@ -11,7 +11,7 @@ export default modelExtend(pageModel, { subscriptions: { setup({ dispatch, history }) { history.listen(location => { - if (pathToRegexp('/post', location.pathname)) { + if (pathToRegexp('/post').exec(location.pathname)) { dispatch({ type: 'query', payload: { diff --git a/src/pages/user/model.js b/src/pages/user/model.js index 612eaff..83ba699 100644 --- a/src/pages/user/model.js +++ b/src/pages/user/model.js @@ -24,7 +24,7 @@ export default modelExtend(pageModel, { subscriptions: { setup({ dispatch, history }) { history.listen(location => { - if (pathToRegexp('/user', location.pathname)) { + if (pathToRegexp('/user').exec(location.pathname)) { const payload = location.query || { page: 1, pageSize: 10 } dispatch({ type: 'query', diff --git a/src/utils/index.js b/src/utils/index.js index dd7efc1..e2d0ee5 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -124,7 +124,7 @@ export function queryLayout(layouts, pathname) { const isMatch = regepx => { return regepx instanceof RegExp ? regepx.test(pathname) - : pathToRegexp(regepx, pathname) + : pathToRegexp(regepx).exec(pathname) } for (const item of layouts) { diff --git a/src/utils/index.test.js b/src/utils/index.test.js index 2118ee4..c4c0b6f 100644 --- a/src/utils/index.test.js +++ b/src/utils/index.test.js @@ -2,17 +2,17 @@ const { pathToRegexp } = require("path-to-regexp") describe('test pathToRegexp', () => { it('get right', () => { - expect(pathToRegexp('/user', '/zh/user')).toEqual( + expect(pathToRegexp('/user').exec('/zh/user')).toEqual( pathToRegexp('/user').exec('/user') ) - expect(pathToRegexp('/user', '/user')).toEqual( + expect(pathToRegexp('/user').exec('/user')).toEqual( pathToRegexp('/user').exec('/user') ) - expect(pathToRegexp('/user/:id', '/zh/user/1')).toEqual( + expect(pathToRegexp('/user/:id').exec('/zh/user/1')).toEqual( pathToRegexp('/user/:id').exec('/user/1') ) - expect(pathToRegexp('/user/:id', '/user/1')).toEqual( + expect(pathToRegexp('/user/:id').exec('/user/1')).toEqual( pathToRegexp('/user/:id').exec('/user/1') ) })