2018.12.22杂七杂八

1,688 阅读2分钟

Q1:如何自动修复Eslint检测出的问题

在react项目中用到Eslint时会检测到代码的很多问题,使用Eslint后即时是很小的诸如缩进、空格等都会提示出现错误而不是警告,此时程序是无法通过的。我们可以通过命令 eslint --fix src(这个是要修复的代码所在的文件),注意用该命令的时候可能会提示缺少eslint-plugin-react,eslint-babel等,我们安装它们即可,然后重新执行命令。执行命令后控制台可能还会提示代码有错误,这时候我们可以手动去修改。

还有一种方法使用webstorm自带的eslint自动修复功能eslint fix,不过在新版的ws中我并没有找到

Q2:Module not found: You attempted to import ****** which falls outside of the project src/ directory. Relative imports outside of src/ are not supported. You can either move it inside src/, or add a symlink to it from project's node_modules/.

意思就是官方会限制src文件夹下文件想引用src文件夹外的文件。

解决方法:

1:项目直接eject命令,如果之前执行了产生了webpack等文件就不用再执行

2:将webpack.config.dev中的ModuleScopePlugin命令注释掉即可

//new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),

Q3:'React' must be in scope when using JSX react/react-in-jsx-scope with “window.React = React” on index.js

模块中没有写import React from 'react'

Q4:webstorm中的只要是index.js命名的文件里面的代码全都是白色的,没有高亮也没有智能提示

代码全是白色的就说明这段代码是没用到的或者是错误的,其实是webstorm没有识别index.js文件,因为webstorm将index.js文件识别成TXT文件了,所以代码时间没用的。

解决方法:

File—Editor—FileTypes找到Text类型,将其中的index.js格式从中取消,apply—ok

img

Q4:Cannot read property 'type' of undefined

这个问题是eslint带来的,相关解决方法参考这篇帖子。我的做法是将webpack中的eslint配置注释掉就不会报错了

Q5:Home does not contain an export named Home

import Home from './layouts/Home`

而不是

import {Home} from './layouts/Home'