问题及解决方案汇总

项目中遇到的问题和找到的解决方案进行汇总清单

问题描述: create-react-app 打包项目run build 增加进度条信息。

解决方案: 使用webpack plugin --- ProgressPlugin

操作: 找到scripts目录下的build.js 增加以下代码

javascript 复制代码
  let compiler = webpack(config);
  
 + compiler.apply(new webpack.ProgressPlugin({
 +     profile: true
 +}));

问题描述: create-react-app脚手架项目怎么添加proxy代理请求。

解决方案: package.json增加代理请求配置。

操作: 找到项目根目录下的package.json,增加以下代码

javascript 复制代码
// 简单单个操作,请求fetch('/api/todos'),将匹配fetch('http://localhost:4000/api/todos')
"proxy": "http://localhost:4000",
// 更多的配置
"proxy": {
    "/api": {
      "target": "<url>",
      "ws": true
      // ...
    }
  }

问题描述: 在使用hashRouter的情况下怎么实现类似锚点跳转

解决方案: 使用Element.scrollIntoView()

操作: 代码示例

javascript 复制代码
const scrollToAnchor = (anchorName) => {
    if (anchorName) {
        // 找到锚点
        let anchorElement = document.getElementById(anchorName);
        // 如果对应id的锚点存在,就跳转到锚点
        if(anchorElement) {
            anchorElement.scrollIntoView();
            // 如果页面有固定header,处理顶部header遮挡title的问题
            const scrolledY = window.scrollY;
            if(scrolledY){
                window.scroll(0, scrolledY - 100);   // 100为header高度
            }
        }
    }
};
相关推荐
前端小巷子19 小时前
JS 打造动态表格
前端·javascript·面试
excel19 小时前
从卷积到全连接:用示例理解 CNN 的分层
前端
UNbuff_019 小时前
HTML 各种事件的使用说明书
前端·html
Mr. Cao code20 小时前
探索OpenResty:高性能Web开发利器
linux·运维·服务器·前端·nginx·ubuntu·openresty
li35741 天前
将已有 Vue 项目通过 Electron 打包为桌面客户端的完整步骤
前端·vue.js·electron
Icoolkj1 天前
VuePress 与 VitePress 深度对比:特性、差异与选型指南
前端·javascript·vue.js
excel1 天前
CNN 分层详解:卷积、池化到全连接的作用与原理
前端
excel1 天前
CNN 多层设计详解:从边缘到高级特征的逐层学习
前端
西陵1 天前
Nx带来极致的前端开发体验——任务编排
前端·javascript·架构
大前端helloworld1 天前
从初中级如何迈入中高级-其实技术只是“入门卷”
前端·面试