码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • react(任意组件之间传值--消息订阅与发布、路由)


    目录

     兄弟组件传值--任意组件之间传值

    fetch发送请求:

    react 路由

    解决样式丢失的问题:

    路由的模糊匹配和严格匹配

    嵌套路由

    向路由组件传参


    前端中文学习网站:印记中文 - 深入挖掘国外前端新领域,为国内 Web 前端开发人员提供优质文档!

    1. 搜索github 用户的案例: 地址: https://api.github.com/search/users?q=xx

     兄弟组件传值--任意组件之间传值

    消息的订阅与发布

    1. 装包: yarn add 'pubsub-js'   

    注意包名不允许有大写

    引入:

    1. import PubSub from 'pubsub-js'
    2. // or when using CommonJS
    3. const PubSub = require('pubsub-js');

    例子:

    1. // create a function to subscribe to topics
    2. var mySubscriber = function (msg, data) {
    3. console.log( msg, data );
    4. };
    5. // add the function to the list of subscribers for a particular topic
    6. // we're keeping the returned token, in order to be able to unsubscribe
    7. // from the topic later on
    8. var token = PubSub.subscribe('MY TOPIC', mySubscriber);
    9. // publish a topic asynchronously
    10. PubSub.publish('MY TOPIC', 'hello world!');
    11. // publish a topic synchronously, which is faster in some environments,
    12. // but will get confusing when one topic triggers new topics in the
    13. // same execution chain
    14. // USE WITH CAUTION, HERE BE DRAGONS!!!
    15. PubSub.publishSync('MY TOPIC', 'hello world!');

    取消订阅:

    1. // add the function to the list of subscribers to a particular topic
    2. // we're keeping the returned token, in order to be able to unsubscribe
    3. // from the topic later on
    4. var token = PubSub.subscribe('MY TOPIC', mySubscriber);
    5. // unsubscribe this subscriber from this topic
    6. PubSub.unsubscribe(token);

    fetch发送请求:

    关注分离的设计思想

    不需要用xhr 发送请求--内置对象

    .then() 的链式调用

    终端promise链:

    .them(

    ()=>{}

    (errro)=>{

    return new Promise(()=>{})

    })

    优化: 把有可能出错的代码写在try中,用catch捕获

     

    react 路由

    前端路由的工作原理: 依靠浏览器的 history

    able

    包裹多个route

    一般组件、路由组件  路由组件会受到一些porps: history,location,match

    标签体内容是一个特殊的标签属性,储存在this.props.children 中

    解决样式丢失的问题:

    1. 使用HashRouter 因为#后面的值默认是前端的,发请求的时候不带给服务器

    2. 将相对路径改为绝对路径

    路由的模糊匹配和严格匹配

    在route中开启精准匹配: exact={true}

    嵌套路由

    1. 注册子路由需要写上父路由的path 值

    2. 路由的匹配是根据注册路由的顺序

    向路由组件传参

    1. 使用 this.props.match.params

    获取:   ‘ :id/:title ’

     2. 传递 sreach 参数

    取: this.props.location.search

    需要借助 ‘querystring’ 这个内置库进行解析

    通过? 和 &传递: (urlencoded 编码)

     【拓】ajax 传递参数的形式:

     3. state 传递,可以不用显示在url地址中

     取: this.props.location.state

     刷新不丢

    路由跳转的两种模式:

    push 、 repalce

    默认是压栈的push操作,开启repalce :

     

  • 相关阅读:
    PDPS软件:机器人控制输送带运行虚拟仿真操作方法
    Selenium中元素定位方法详细介绍
    MySQL自定义函数(User Define Function)开发实例——发送TCP/UDP消息
    倍福PLC旋切基本原理和应用例程
    vue3使用windicss
    大健康老年公寓管理系统设计与实现-计算机毕业设计源码+LW文档
    flutter开发实战 - inappwebview设置cookie
    汉纳西点:100天成功打造大连行业最大单体店,创造一个商业传奇
    从HTTP到Tomcat:揭秘Web应用的底层协议与高性能容器
    Python基础学习(11)常用模块
  • 原文地址:https://blog.csdn.net/qq_52179917/article/details/127977921
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号