您好,登錄后才能下訂單哦!
本篇內容介紹了“react項目中如何使用插件配置路由”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
推薦使用插件完成
https://www.npmjs.com/search?q=react-router-waiter
react-router v6 路由統一管理 及 路由攔截方案。
cnpm i --save-dev react-router-waiter "react-router-waiter": "^1.1.7",
//引入路由 import { BrowserRouter as Router } from "react-router-dom"; //封裝了內部的Routes組件 import RouterView from "react-router-waiter"; //引入路由配置和守衛 import { routes, beforeRouter } from "./router/index"; export default () => { return ( <> <Router> <RouterView routes={routes} onRouteBefore={beforeRouter}></RouterView> </Router> </> ); };
react-router-waiter組件提供了Routes和onrouterbefore API
1.Routes 路由配置
2.onRouteBefore 路由前置守衛
//創建路由配置 export const routes = []; //創建路由前置守衛 export const beforeRouter = () => {};
//創建路由配置 //引入同步組件 import Index from "../views/Index"; import Login from "../views/Login"; import NotFound from "../views/Not-found"; //component屬性使用()=>import //element 同步 export const routes = [ { path: "/admin", element: <Index />, meta: { name: "系統首頁", }, }, { path: "/login", element: <Login />, }, { path: "/not-found", element: <NotFound />, }, { path: "/", redirect: "/admin", }, { path: "*", redirect: "/not-found", }, ]; //react項目中配置 同步路由組件 component屬性換位 element屬性 走同步 //如果組件使用異步載入 使用component //創建路由配置 //引入同步組件 import Index from "../views/Index"; import Login from "../views/Login"; import NotFound from "../views/Not-found"; //component屬性使用()=>import //element 同步 export const routes = [ { path: "/admin", component: () => import("../views/Index"), meta: { name: "系統首頁", }, }, { path: "/login", component: () => import("../views/Login"), }, { path: "/not-found", component: () => import("../views/Not-found"), }, { path: "/", redirect: "/admin", }, { path: "*", redirect: "/not-found", }, ] //()=>import(“***”) //底層 React.lazy(()=>import(""));
{ path: '/', element: <PageLayout />, // 父級的公共組件使用element配置 children: [ ... // 子級可以繼續使用component配置 ] },
二級路由寫法
{ path: "/admin", // element: <Index />, component: () => import("../views/Index"), children: [ { path: "index", component: () => import("../views/children/Index"), }, { path: "user", component: () => import("../views/children/User"), }, ], meta: { name: "系統首頁", }, }, //使用該插件避免閃屏 //建議父級同步 子集懶加載 { path: "/admin", element: <Index />, children: [ { path: "index", component: () => import("../views/children/Index"), }, { path: "user", component: () => import("../views/children/User"), }, ], meta: { name: "系統首頁", }, },
/** * @param {string} pathname 當前路由路徑 * @param {object} meta 當前路由自定義meta字段 * @return {string} 需要跳轉到其他頁時,就返回一個該頁的path路徑,或返回resolve該路徑的promise對象 */ const onRouteBefore = ({ pathname, meta }) => { // 示例:動態修改頁面title if (meta.title !== undefined) { document.title = meta.title } // 示例:判斷未登錄跳轉登錄頁 if (meta.needLogin) { if (!isLogin) { return '/login' } } } export default onRouteBefore
使用守衛做登錄認證
//創建路由前置守衛 export const beforeRouter = ({ pathname, meta }) => { console.log(pathname, meta); //檢測用戶是否登錄 let token = localStorage.getItem("_token"); console.log(token); if (token) { if (pathname == "/login") { return "/admin"; } } else { return "/login"; } };
中文文檔
https://www.redux.org.cn/
Redux 是 JavaScript 狀態容器,提供可預測化的狀態管理。
Redux 由 Flux 演變而來
安裝redux
npm install --save redux
附加包
npm install --save react-redux npm install --save-dev redux-devtools
redux不是react內置庫屬于第三方庫文件。
安裝redux "redux": "^4.2.1",
main.js直接使用redux
//操作redux //1.引入redux redux插件使用的是單暴漏模式 不是export default //2.解出API 18里面使用legacy_createStore 之前使用createstore API //3.使用api 創建唯一store(store對象) 唯一是整個項目就這一個store對象 import { legacy_createStore } from "redux"; //4.創建store對象 參數必填 reducer 函數 const store = legacy_createStore(); console.log(store);
定義reducer函數,創建store對象
import { legacy_createStore } from "redux"; //4.創建store對象 參數必填 reducer 函數 //5.定義reducer 函數 function reducer() {} const store = legacy_createStore(reducer); console.log(store);
創建store對象調用createStore API 默認執行reducer函數
function reducer() { console.log("測試"); } const store = legacy_createStore(reducer); console.log(store);
默認執行reducer的作用是,創建store執行reducer獲取默認的狀態值。
//action 為觸發的動作指令 function reducer(state, action) { console.log("測試", state, action); } const store = legacy_createStore(reducer); console.log(store); //redux底層執行動作 {type: '@@redux/INITt.0.e.r.j.b'} 執行該動作執行reducer獲取默認值。
state設置初始狀態值
//定義初始化狀態 let initialState = { count: 0, isShow: false, }; //reducer形參 //state狀態值 //action 為觸發的動作指令 function reducer(state = initialState, action) { console.log("測試", state, action); return state; } const store = legacy_createStore(reducer); //獲取store state狀態值 console.log(store.getState());
使用dispatch觸發action執行reducer修改state狀態值
dispatch: ƒ dispatch(action)
//錯誤寫法
store.dispatch("INCREMENT");
dispatch({type:"動作"})
import { legacy_createStore } from "redux"; //4.創建store對象 參數必填 reducer 函數 //5.定義reducer 函數 //定義初始化狀態 let initialState = { count: 0, isShow: false, }; //reducer形參 //state狀態值 //action 為觸發的動作指令 function reducer(state = initialState, action) { //獲取action type let { type } = action; switch (type) { case "INCREMENT": state.count++; return state; default: return state; } } const store = legacy_createStore(reducer); //獲取store state狀態值 console.log(store, store.getState()); //使用store對象api 執行動作觸發修改state狀態值 store.dispatch({ type: "INCREMENT" }); console.log(store.getState());
使用附加包 npm install --save react-redux "react-redux": "^8.0.5",
項目中用法:
//引入react-redux //Provider reactredux內置組件限定范圍傳值 import { Provider } from "react-redux"; ReactDOM.createRoot(document.getElementById("root")).render( <React.StrictMode> {/* 將store對象關聯到整個react項目 */} <Provider store={store}> <App /> </Provider> </React.StrictMode> );
關聯之后函數組件中使用方案
//引入react-redux //useDispatch dispatch 觸發action //useSelector getState獲取狀態值 import { useDispatch, useSelector } from "react-redux"; export default () => { return <>系統首頁</>; }; //函數組件使用使用react-redux hook 完成狀態值修改 //引入react-redux //useDispatch dispatch 觸發action //useSelector getState獲取狀態值 import { useDispatch, useSelector } from "react-redux"; export default () => { //獲取store狀態值 let { count } = useSelector((state) => state); let dispatch = useDispatch(); let increment = () => { dispatch({ type: "INCREMENT" }); }; let decrement = () => { dispatch({ type: "DECREMENT" }); }; return ( <> 系統首頁-{count} <button onClick={increment}>++</button> <button onClick={decrement}>--</button> </> ); };
存在一個問題,state狀態值更新界面沒有更新
原因試store對象中監聽不到state對象變更
修改在reducer中修改完成state狀態值之后需要斷鏈 return { ...state }; return Object.assign({}, state);
“react項目中如何使用插件配置路由”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。