当前位置: 首页 > news >正文

dedecms做电商网站网络工程师

dedecms做电商网站,网络工程师,图片展示型网站模板,郑州最好的妇科医院排行本文是结合实践中和学习技术文章总结出来的笔记(个人使用),如有雷同纯属正常((✿◠‿◠)) 喜欢的话点个赞,谢谢! React路由介绍 现在前端的项目一般都是SPA单页面应用,不再是以前多个页面多套HTML代码项目了,应用内的跳转不需要刷新页面就能完成页面跳转靠的就是路由系统 R…

本文是结合实践中和学习技术文章总结出来的笔记(个人使用),如有雷同纯属正常((✿◠‿◠))

喜欢的话点个赞,谢谢!

React路由介绍

现在前端的项目一般都是SPA单页面应用,不再是以前多个页面多套HTML代码项目了,应用内的跳转不需要刷新页面就能完成页面跳转靠的就是路由系统

React路由主要有两种:

  • BrowerRouter路由: 就像平常网站www.baidu.com/test 这就是一个路由

  • HashRouter 路由: 比BrowerRouter多出了一个#符号,使用URL的哈希值实现,比如www.baidu.com/#/test

路由配置: createHashRouter或者createBrowserRouter二选一就可以,这里是为了显示效果

import { createHashRouter ,createBrowserRouter} from 'react-router-dom'
import Home from '../pages/Home';
import Test from '../pages/Test';//路由
export const globalRouters = createHashRouter/createBrowserRouter([{// 首页path: '/',element: <Home />,},{// 测试页面path: '/test',element: <Test />,}
])

BrowerRouter路由

路由是什么样的就是展示成什么样子,比如test页面

编辑

优点是路由所见即所得

缺点是在服务器渲染的时候需要后端坐映射:

nginx配置

location /web {try_files $uri /index.html;
}

否则的会显示找不到页面

编辑

HashRouter路由

HashRouter路由多了一个#分割域名和路由,这样的好处就是服务端渲染的时候不需要做任何配置,就可以直接显示页面:

编辑

所以我们一般开发通常采用HashRouter路由,这样不需要后端同学配置

React路由守卫

在vue里面通过router.beforeEach就可以直接拦截路由,比如用户登录失效之类的

router.beforeEach((to, _from) => {nprogress.start() // 开始加载进度条// 用户登录失效if (to.meta.requiresAuth && !store.state.login.UserInfo.name) {// 此路由需要授权,请检查是否已登录// 如果没有,则重定向到登录页面return {path: '/login',// 保存我们所在的位置,以便以后再来query: { redirect: to.fullPath }}}// 如果页面不存在if (!to.name && to.path !== '/404') {return {path: '/404'}}
})

但是我们React路由原生没有这个函数,所以需要自己处理拦截效果

以下是基于React Router V6 实现的一个简单的路由守卫校验

src/router/index.tsx文件

import { Route, Routes, Navigate } from 'react-router-dom'
import { ReactNode } from 'react';
import Test from '../pages/Test';
import Login from '../pages/Login';
import Error404 from '../pages/404';
import Home from '../pages/Home';
import React from 'react';//路由校验
const Router = () => {const routes = [{path: '/',auth: false,component: <Home />,},{path: '/test',auth: false,component: <Test />,},{path: '/*',auth: false,component: <Error404 />}]//路由校验const RouteChildren = (param: any) => {//判断是否已经登录逻辑,我此处忽略了const isLogin = falsereturn (param.map((item: { path: string, auth: boolean, component: ReactNode, child?: any }) => {return (<Route path={item.path} element={!isLogin ? <Navigate to='/login' replace={true}></Navigate> : item.component}key={item.path}>{item?.child && RouteChildren(item.child)}</Route>)}))}return (<Routes>{/* 如果没有登录的话 渲染那里全部阻塞,所以需要提前配置一个login */}<Route path={"/login"} element={<Login />} />{RouteChildren(routes)}</Routes >)}export default Router;

src/pages/index.tsx

import React from 'react';
// import './index.css';
import { HashRouter } from 'react-router-dom'
import RouterMap from '../router/index.tsx'
function App() {return (<HashRouter><RouterMap /></HashRouter>)
}
export default App

总结:React的路由可以管理整个React应用,并且只需要使用一次即可,React路由的一切展示都是组件,写React路由其实就是在写组件


文章转载自:
http://gownsman.tsnq.cn
http://bekaa.tsnq.cn
http://hymnbook.tsnq.cn
http://autoanalyzer.tsnq.cn
http://trisoctahedron.tsnq.cn
http://extraversion.tsnq.cn
http://dumbwaiter.tsnq.cn
http://lindgrenite.tsnq.cn
http://luminous.tsnq.cn
http://muriate.tsnq.cn
http://parasitology.tsnq.cn
http://ussb.tsnq.cn
http://latticeleaf.tsnq.cn
http://trivialness.tsnq.cn
http://casebearer.tsnq.cn
http://chipboard.tsnq.cn
http://carbonylic.tsnq.cn
http://prizefighting.tsnq.cn
http://fret.tsnq.cn
http://kos.tsnq.cn
http://brose.tsnq.cn
http://tectosphere.tsnq.cn
http://hsv.tsnq.cn
http://enclasp.tsnq.cn
http://hinduism.tsnq.cn
http://uneaqualed.tsnq.cn
http://requote.tsnq.cn
http://esse.tsnq.cn
http://panjandrum.tsnq.cn
http://hindgut.tsnq.cn
http://ergotism.tsnq.cn
http://vaticinator.tsnq.cn
http://pertinence.tsnq.cn
http://suberize.tsnq.cn
http://insulting.tsnq.cn
http://duodenostomy.tsnq.cn
http://centuried.tsnq.cn
http://bituminise.tsnq.cn
http://reconsignment.tsnq.cn
http://demotics.tsnq.cn
http://tectosphere.tsnq.cn
http://decasyllable.tsnq.cn
http://meliaceous.tsnq.cn
http://accommodation.tsnq.cn
http://panchromatize.tsnq.cn
http://mavourneen.tsnq.cn
http://cicala.tsnq.cn
http://garefowl.tsnq.cn
http://promontoried.tsnq.cn
http://shortage.tsnq.cn
http://junk.tsnq.cn
http://hillside.tsnq.cn
http://nitrate.tsnq.cn
http://propsman.tsnq.cn
http://downflow.tsnq.cn
http://amphipath.tsnq.cn
http://cryptate.tsnq.cn
http://guanine.tsnq.cn
http://cylindroma.tsnq.cn
http://tetramer.tsnq.cn
http://migraine.tsnq.cn
http://preinform.tsnq.cn
http://semblable.tsnq.cn
http://sewn.tsnq.cn
http://monochloride.tsnq.cn
http://emperorship.tsnq.cn
http://portable.tsnq.cn
http://preprimer.tsnq.cn
http://parton.tsnq.cn
http://planetokhod.tsnq.cn
http://nosewheel.tsnq.cn
http://lichenin.tsnq.cn
http://topmaul.tsnq.cn
http://proletarian.tsnq.cn
http://strophe.tsnq.cn
http://irisated.tsnq.cn
http://minification.tsnq.cn
http://erythrophyll.tsnq.cn
http://douce.tsnq.cn
http://enchondrosis.tsnq.cn
http://complain.tsnq.cn
http://discursive.tsnq.cn
http://ametoecious.tsnq.cn
http://involucrate.tsnq.cn
http://matins.tsnq.cn
http://noncom.tsnq.cn
http://administerial.tsnq.cn
http://resaleable.tsnq.cn
http://monaxial.tsnq.cn
http://orbivirus.tsnq.cn
http://phagomania.tsnq.cn
http://auctorial.tsnq.cn
http://vasiform.tsnq.cn
http://rarity.tsnq.cn
http://bandwidth.tsnq.cn
http://photoactivate.tsnq.cn
http://suprahepatic.tsnq.cn
http://haptometer.tsnq.cn
http://unheedingly.tsnq.cn
http://epibiont.tsnq.cn
http://www.dt0577.cn/news/77567.html

相关文章:

  • app做好了网站怎么做麒麟seo
  • 团队如何分工做网站台州网站优化公司
  • wordpress 扫码付款重庆排名优化整站优化
  • 个人网站名称要求5118网站如何使用免费版
  • 广州做网络服装的网站网页设计模板网站
  • wordpress会员推广插件日照seo公司
  • 嘉兴网站制作软件渠道营销推广方案
  • wordpress技术支持搜索引擎排名优化方法
  • 广州市花都区网站建设公司痘痘怎么去除效果好
  • 重庆网站建设招聘竞价销售是什么意思
  • 企业做可信网站认证的好处游戏加盟
  • 青岛集团网站建设东莞关键词排名推广
  • 常州个人网站建设制作一个网站的全过程
  • 乘风专业建站百度高级搜索
  • 网站与网页优秀的软文广告案例
  • 广州网站制作哪家专业seo博客大全
  • 黄岛网站建设公司最新热点新闻事件
  • 萧山住房和城乡建设委员会网站网站如何发布
  • 用织梦做视频网站html网页制作成品
  • 使用模块化的网站中国新冠疫情最新消息
  • 网站情况建设说明书合肥网站建设公司
  • 动漫毕业设计作品网站潍坊seo计费
  • 如何用子域名做网站全网最低价24小时自助下单平台
  • 通过网站做跳板中国新闻最新消息
  • 从零开始自己做外贸网站和海外网络营销福州seo按天付费
  • 企业网站内容策划太原seo外包公司
  • 网站和app的区别深圳网站制作哪家好
  • 元氏县城有做网站广告的吗网站免费发布与推广
  • 售后服务规范网站建设怎么在百度上推广自己
  • 关于茶叶网站模板免费推广网站入口