开发过程中遇到的问题

简介: try catch 在接口中使用注意使用 try catch 的使用无论是在 try 中的代码还是在 catch 中的代码性能消耗都是一样的。需要注意的性能消耗在于 try catch 中不要直接塞进去太多的代码(声明太多的变量),最好是吧所有要执行的代码放在另一个 function 中,通过调用这个 function 来执行。
  1. try catch 在接口中使用注意

使用 try catch 的使用无论是在 try 中的代码还是在 catch 中的代码性能消耗都是一样的。需要注意的性能消耗在于 try catch 中不要直接塞进去太多的代码(声明太多的变量),最好是吧所有要执行的代码放在另一个 function 中,通过调用这个 function 来执行。针对第二点,可以查看 ECMA 中关于 try catch 的解释,在代码进入 try catch 的时候 js引擎会拷贝当前的词法环境,拷贝的其实就是当前 scope 下的所有的变量。建议在使用 try catch 的时候尽量把 try catch 放在一个相对干净的 scope 中,同时在 try catch 语句中也尽量保证足够少的变量,最好通过函数调用方式来 try catch。

   try {
        yield call(services.deleteComment, i, commentid);
        Toast.success('删除成功',1.5);
        yield put(routerRedux.push('/notebooks/comment'));
      } catch (e) {
        Toast.fail('删除失败',1.5);
      }

2.在开发过程中程序会在不同手机的浏览器进行使用,偶尔会在操作中出现以下现象:

image.png

点着点着会引发浏览器自带选中文字状态。
解决方案(不可复制,不可选择)

 body {
    -webkit-user-select:none;
    -moz-user-select:none;
    -ms-user-select:none;
    user-select:none;
  }
  1. 在react+antd+dva的项目中集成html2canvas截图功能

首先在已经创建好得项目的文件夹下找到 index.ejs 这个文件

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>111</title>
</head>
<body>
  <div id="root" style="background: #efefef"></div>
</body>
<script src="https://cdn.bootcss.com/html2canvas/0.5.0-beta4/html2canvas.js"></script>
</html>

导入在线的/html2canvas.js文件,然后在写一个页面,正文如下:

screenshot(event){// 截图并保存到本地
         html2canvas(document.body).then(function(canvas) {    
            canvas.id = "mycanvas";         
            let newImg = document.createElement("img");  
            let newSrc = document.createElement("a");  
            let type = 'png';
            let imgData = canvas.toDataURL(type);
            let saveFile = function(data, filename){
            let save_link = document.createElementNS('http://www.x3.org/1999/xtml', 'a');
                save_link.href = data;
                save_link.download = filename;
                let event = document.createEvent('MouseEvents');
                event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                save_link.dispatchEvent(event);
            };
            let filename = '图片名' + (new Date()).getTime() + '.' + 'png';
            let ua = navigator.userAgent.toLowerCase();   
                  saveFile(imgData,filename);
                  newImg.src =  imgData;   
          })    
    }

return(
<div>
        <div id="textArea"> 11111111111</div>
        <button onclick={(event) => this.screenshot(event)}>截图</button>
</div>
)

4.实时通讯(该demo只是前端的部分)

css:

         .labText {
             min-height: 230px;
             border: solid 1px #eee;
             width: 1000px;
             margin: 10px auto;
             height: 300px;
             overflow-y: scroll;
         }

html:

<input type="text" id="txtSendText" /><input id="btnSend" type="button" value="send" />
<div id="labText">消息:</div> 
<script src="jquery-1.8.3.min.js"></script>
<script src="jquery.signalR-2.1.2.min.js"></script>
<script src="http://192.168.118.100:8080/signalr/hubs"></script>
 // 此文件是在c#中下载signalR自动生成的,最好后端放在服务器上,你直接写路径。

js:

     function showMessage(msg) {
            $("#labText").append("<div>" + msg + "</div>");
        }
     $(function () {
        // Declare a proxy to reference the hub.
        var hub = $.connection.Hub;
        console.log(hub)
        $.connection.hub.qs = {patientName:"jack",id:9000};
        $.connection.hub.url = "http://192.168.118.100:8080/signalr"; // 是服务器上的文件

        // Create a function that the hub can call to broadcast messages.
        hub.client.sendMessage = function (msg) {
            $("#labText").append("<div>" + msg+"</div>");

        };
        hub.client.BroadcastMessage = (msg) => {
            showMessage(msg);
        };
        // Start the connection.
        $.connection.hub.start({jsonp:true}).done(() => {
            alert("启动成功!");
            $("#btnSend").click(function() {
                var msg = $("#txtSendText").val();
                console.log(msg)
                hub.server.sendToAll(msg);
                $("#txtSendText").val("").focus();
            });
            // hub.server.hello();
        });
        $.connection.hub.error((error) => {
            // showMessage("启动成功!");
            console.log(error)
            alert("启动失败!")
        });

    });

5.this.setState的写法表示不同的含义
驱动组件渲染的过程中除了prop, 还有state,state代表的是组件内部的状态。由于React组件不能修改传入的prop,所以需要记录自身数据变化,就要使用state。

示例:

 this.setState({...owner, name: ‘Jason'}) 
//   相当于把name键值对添加到owner对象里(把owner对象里面的name的值替换成Jason)     
 this.setState(...owner, {name: ‘Jason’}) 
//   把整个owner对象替换为 name: Jason

举例:

     let aWithOverrides = { …a, x: 1, y: 2 };
    //  等同于
     let aWithOverrides =  { …a, …{ x: 1; y: 2} };
    //  等同于
     let x = 1, y = 2, aWithOverrides = { ...a, x, y };
    //  等同于
     let aWithOverrides = Object.assign({}, a, { x: 1, y: 2 });
  1. react中组件被重新渲染(闪屏现象)

简介:ShouldCompleteUpdate,下文简称SCU,就是指明什么时候component(组件)需要进行更新。

示例:

 constructor(props) {
        super(props);
        this.onClickButton = this.onClickButton.bind(this);
        this.state = { count: 0};
  }
 shouldComponentUpdate(nextProps, nextState) {
      if (this.state.count !== nextState.count) {
        return true;
      }
      return false;
  }
onClickButton() {
        this.setState({ count: this.state.count + 1 });
  }
// 其中的state.count的值要被改变的,我们可以直接用SCU
// 组件仅仅会校验state.count,如果这些值都不会改变,那么组件就不会有更新。

举例:

  constructor(props) {
      super(props);
       // console.log(this.props);
      this.onClickIncrementButton = this.onClickIncrementButton.bind(this);
      this.onClickDecrementButton = this.onClickDecrementButton.bind(this);
      // this.foo = ::this.foo 其他教程中可以这样去编写它
      // this.foo = this.foo.bind(this)

      this.state = {
        count: props.initValue // initValue 是一个可选的props 
      }
    }

  /*
  getInitialState() {
    getInitialState的返回值用来初始化组件的this.state,只有用React.createClass方法创造的组件类才会起作用,
    在这里这个函数是根本不起作用的
    console.log('enter getInitialState');
  }

  getDefaultProps() {

    console.log('enter getDefaultProps');
  }
  */
  componentWillReceiveProps(nextProps) {}

  componentWillMount() {}

  componentDidMount() {}

  onClickIncrementButton() {
    this.setState({count: this.state.count + 1}); 
  }

  onClickDecrementButton() {
    this.setState({count: this.state.count - 1});
  }

  shouldComponentUpdate(nextProps, nextState) {
    return (nextProps.caption !== this.props.caption) ||
           (nextState.count !== this.state.count);
  }
// 
当页面重新被渲染之前,把一些不必要的和没有渲染的节点进行处理。
因为页面的两部分分别渲染5000个节点,从1-5000。
当点击按钮之后,第二部分的节点会更新,重新渲染从2-5001的数字,但是第一部分保持不变。
所以说SCU是在组件没被二次渲染之前删掉一些浪费的渲染。
shouldComponentUpdate这个函数是必须是在react的生命周期函数中去调用。
//

7.在react中导入iconfont图标字体遇到的状况

首先:
(1)去阿里矢量图官网http://www.iconfont.cn/添加你所喜欢的iconfont,然后你的购物车里面就是你添加的iconfont。

one.png

(2)把购物车中iconfont添加已有的项目qqq中去,如果没有的可以在右上脚新建新的项目

two.png

(3)把项目的代码复制下来,跟本地添加 iconfont一样。
three.jpg

示例如下:
(3)index.css中内容

// 本地和导入项目 这个是固定的
.iconfont{
    font-family:"iconfont";
    font-size:40px;
    font-style:normal;
}

//这个就是你复制在iconfont上的代码链接(你添加在项目中的iconfont)
@font-face {
  font-family: 'iconfont';  /* project id 499958 */
  src: url('//at.alicdn.com/t/font_499958_759xzvyfw16k1emi.eot');
  src: url('//at.alicdn.com/t/font_499958_759xzvyfw16k1emi.eot?#iefix') format('embedded-opentype'),
  url('//at.alicdn.com/t/font_499958_759xzvyfw16k1emi.woff') format('woff'),
  url('//at.alicdn.com/t/font_499958_759xzvyfw16k1emi.ttf') format('truetype'),
  url('//at.alicdn.com/t/font_499958_759xzvyfw16k1emi.svg#iconfont') format('svg');
}

(4)在页面上的使用:

import styles from './index.css';// 引入CSS

render() {
    return (
      <div>
              <i className="iconfont">&#xe617;</i>// 老版本的写法
              <i className={styles.iconfont}>&#xe617;</i>// 目前的写法
      </div>
     )
 }

8.react(dva)+antd创建项目之后的路由问题

(1)之前低版本的路由是可以有嵌套关系的

示例如下:

  return (
      <Router history={history}>
          <Route path="/" component={Layout}>
              <IndexRoute component={HealthyPage} />
              <Route path="department" component={DepartPage} />
              <Route path="signin" component={SigninPage} />
              <Route path="signup">
                    <IndexRoute component={SignupMobilePage} />
                    <Route path="2" component={SignupPasswordPage} />
                    <Route path="3" component={SignupUserInfoPage} />
              </Route>
              <Route path="forgot">
                    <IndexRoute component={ForgotPasswordPage} />
                    <Route path="reset" component={ResetPasswordPage} />
              </Route>
              <Route path="signauth" component={SignAuthPage} />
              <Route path="doctor" onEnter={() => requireAuth(app._store)}>
                    <IndexRoute component={DoctorPage} />
                    <Route path=":id" component={ConversationPage} />
              </Route>
              <Route path="healthy" onEnter={() => requireAuth(app._store)}>
              </Route>
      </Router>
  )

使用IndexRoute有利于代码分离,也有利于使用React Router提供的各种API。IndexRoute组件没有路径参数path。

(2) 当前版本的路由是不可以嵌套的

示例如下:

 <Router history={history}>
      <Switch>
          <Route path="/" exact component={SinAuthPage} />
          <Route path="/IndexPage" exact component={IndexPage} />
          <Route path="/Clickmobile" exact component={Clickmobile} />
          <Route path="/IndexPage/chd" exact component={chd} />
          <Route path="/IndexPage/pre" exact component={pre} />
          <Route path="/IndexPage/dia" exact component={dia} />
          <Route path="/IndexPage/phy" exact component={phy} />
          <Route path="/IndexPage/partum" exact component={partum} />
      </Switch>
    </Router>

现在是变的是单个页面要单个单个的去写,不能像之前的嵌套关系比较复杂啦。如果像之前那样去写的话会报以下的错误:
Syntax error: Expected corresponding JSX closing tag for <Switch>

什么意思呢?意思就是说:相关的jsx语法有问题 直接报的是代码有问题。

目前还有找到相对应的解决方案。

目录
相关文章
|
传感器 Ubuntu 算法
【6. 激光雷达接入ROS】(1)
【6. 激光雷达接入ROS】(1)
1196 0
|
9月前
|
存储 监控 Java
分布式调用三大基石:超时、重试、幂等的架构级落地规范与全场景避坑指南
本文深入解析分布式调用稳定性三大基石:超时(设生死线、分层预算、中断执行)、重试(限次数/退避/幂等前提)与幂等(唯一键、原子校验、结果复用),结合全链路透传、AOP实现及高频避坑指南,提供可落地的架构级协同方案。
819 6
|
10月前
|
传感器 人工智能 算法
从春晚舞台到全球赛场:中国人形机器人,到底走到了哪一步?
2026年央视春晚创纪录启用超200台国产人形机器人,宇树科技(武术集群)、魔法原子(街舞+熊猫狗)、松延动力(仿生小品)、银河通用(具身智能微电影)四企领衔,覆盖运动控制、集群协同、情感交互、自主决策四大技术高地,标志中国人形机器人迈入规模化落地元年。(239字)
915 2
|
自然语言处理 监控 API
速卖通商品详情API秘籍!轻松获取SKU属性数据
速卖通商品详情API(aliexpress.item.get)支持通过编程获取商品标题、价格、SKU、库存、销量、物流模板、评价及店铺信息,适用于价格监控、选品分析等场景。接口支持多语言返回,采用AppKey+AppSecret+Token认证,需签名验证,确保安全调用。
|
存储 人工智能 运维
为什么新兴行业更需要 NaaS? 2025-09-08 14:40:38
在数字化浪潮下,AI、短剧、电商、数字文娱等新兴行业迅速崛起,业务追求敏捷高效,但网络却常成瓶颈。传统网络部署慢、成本高、体验差,难以满足其快速发展需求。NaaS(网络即服务)应运而生,提供分钟级部署、弹性带宽、合规加速与专业运维支持,助力企业专注创新,真正实现“敢快跑”。
|
安全 虚拟化 Windows
Windows Server 2016 中文版、英文版下载 (2025 年 2 月更新)
Windows Server 2016 中文版、英文版下载 (2025 年 2 月更新)
842 17
Windows Server 2016 中文版、英文版下载 (2025 年 2 月更新)
|
存储 人工智能 自然语言处理
基于MCP的桥梁设计规范智能解析与校审系统构建实践
本文探讨了在桥梁设计领域应用AI技术的实践与思考,针对传统设计中规范查阅效率低、理解偏差大等问题,提出基于MCP协议构建智能解析与校审系统。系统通过PDF解析、知识图谱构建及自然语言处理等技术,实现规范条文的结构化存储和智能化应用,大幅提升设计效率与准确性。开发过程中克服了多模态文档解析、专业术语理解等挑战,并通过迭代优化持续改进性能。未来该系统有望扩展至更多工程领域,推动设计流程智能化升级。
653 8
|
供应链 安全 算法
签名不等于可信:详解PE数字签名校验的漏洞与主动规避方案
本文探讨了CVE-2013-3900漏洞的原理及其影响,该漏洞允许攻击者在不破坏数字签名有效性的情况下,向PE文件中添加恶意代码。漏洞源于Windows对签名数据后附加数据的校验缺失,导致恶意软件可伪装成合法软件。文章分析了WinVerifyTrust函数的工作机制及修复方法,包括通过注册表启用严格签名校验(EnableCertPaddingCheck)。同时,提出了通过hook注册表函数主动规避漏洞的方法,确保安全软件在未启用严格校验时仍能检测潜在威胁。此研究对提升PE文件签名安全性具有重要意义。
|
编译器 数据中心 开发者
安谋科技(Arm China)别再平:Arm Neoverse 软件生态介绍
2023年8月15日,系列课程第二节《Arm Neoverse 软件生态介绍》正式上线,由安谋科技(Arm China)高级软件经理别再平主讲,内容涵盖:Arm Neoverse 介绍,Arm Neoverse 软件生态系统,Arm Neoverse 软件迁移。
安谋科技(Arm China)别再平:Arm Neoverse 软件生态介绍