日常中遇到的一些报错的解决方案

160 阅读2分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第3天,点击查看活动详情

1、安装依赖报错

error fatal: unable to connect to github.com:
error github.com[0: IP地址]: errno=Unknown error
error
error
error exited with error code: 128
verbose exit [ 1, true ]

解决方案 输入命令 git config --global url."https://".insteadOf git://

2、vue-cli创建项目选择yarn或者npm的问题

vue-cli首次创建项目时你选择了 Use Yarn,那么会自动保存一个配置文件在C盘的用户目录下,比如我的C:\Users\Administrator\下会有一个文件.vuerc,在下次创建项目时会默认选择首次创建选择的创建工具。

若想更改创建工具,解决方案:

  • 修改.vuerc文件
{
  "useTaobaoRegistry": false,
  "packageManager": "npm"
}
  • .vuerc文件删掉,创建时会重新让你选择创建工具

3、关闭 elsint 检测在vue.config.js加上lintOnSave: false

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
  lintOnSave: false
})

4、报错Cannot find module '@/' or its corresponding type declarations

解决方案:把项目拖到vs工作区的第一位

5、处理下载文件错误提示

拦截blob格式下载报错提示

  • Object.prototype.toString.call(data) === '[object Blob]'判断是否为blob格式通过
  • 通过blob格式转成json格式,获取success判断是否成功,进行后续提示处理
if (
  Object.prototype.toString.call(data) === '[object Blob]' &&
  data.type.includes('application/json')
) {
  const reader = new FileReader();
  reader.onload = function () {
    const res = reader.result as string;
    const { data: error, success } = JSON.parse(res); // blob格式转成json
    if (success) {
      ElMessage({
        type: 'success',
        message: error || '操作成功',
        showClose: true,
      });
    } else {
      ElMessage({
        type: 'error',
        message: error.errorMsg || '未知错误',
        showClose: true,
      });
    }
  };
  reader.readAsText(data);
  return Promise.reject(data);
}

6、Taro小程序cannot read property 'params' of null报错

点击选择图片如果使用 const { orderNo= '' } = Taro.getCurrentInstance().router.params

获取路由对应params中的orderNo值会出现以下报错,在ios系统中并没有出现这样的报错,但在安卓系统大部分机器都复现报错

解决方案:使用useMemo(fn, arr) arr为[]时,fn只执行一次;当arr不为[]时,如[val],当val发生变化时,fn才会再次执行。

  • useMemo是针对一个函数,是否多次执行
  • useMemo主要用来解决使用React hooks产生的无用渲染的性能问题
const { orderNo } = useMemo(() => {
  returnTaro.getCurrentInstance().router.params;
}, []);