使用react-router-config配置路由

kyooo0發表於2021-01-02

安裝react-router-config
yarn add react-router-config


App.js

import { renderRoutes } from 'react-router-config';
import { HashRouter } from 'react-router-dom';

//routes 是路由配置檔案
import routes from "./router";
...

//renderRoutes需要Router包裹
<HashRouter>
	{renderRouters(routes)}
</HashRouter>

=>
router.js

//配置頁面

//匯入頁面
import HYDiscover from "@/pages/discover";
import HYFriends from "@/pages/friends";
import HYMine from "@/pages/mine";

const routes = [
    {
        path: '/',
        exact: true,
        component: HYDiscover
    },
    {
        path: '/friends',
        component: HYFriends
    },
    {
        path: '/mine',
        component: HYMine
    }
]

export default routes

相關文章