javascript 首选默认导出 eslint 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48750574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:15:24  来源:igfitidea点击:

Prefer default export eslint error

javascriptreactjs

提问by David Brierton

I am getting this eslint error:

我收到这个 eslint 错误:

Prefer default export

首选默认导出

import React, { Component } from 'react';

class HomePage extends Component {
  render() {
    return (
      <div className="Section">HomePage</div>
    );
  }
}

export { HomePage };

I have tried doing:export { default as Homepage };

我试过这样做:export { default as Homepage };

and then I get a fatal parsing error.

然后我得到一个致命的解析错误。

Then I changed it to:

然后我把它改成了:

export default HomePage;

Which clears the eslint error.

这清除了 eslint 错误。

But then throws:

但随后抛出:

'./HomePage' does not contain an export named 'HomePage'.

“./HomePage”不包含名为“HomePage”的导出。

Because I am calling HomePage like this: import { HomePage } from './HomePage';

因为我是这样调用 HomePage 的: import { HomePage } from './HomePage';

If I remove the brackets then I get this error:

如果我删除括号,则会收到此错误:

"export 'default' (imported as 'HomePage') was not found in './HomePage'

“在 './HomePage' 中找不到导出 'default'(导入为 'HomePage')

import HomePage from './HomePage';
<PrivateRoute exact path="/" component={HomePage} />

What would be the proper way of changing this to the preferred default export?

将其更改为首选默认导出的正确方法是什么?

回答by RIYAJ KHAN

From eslint-plugin-import

eslint-plugin-import

When there is only a single export from a module, prefer using default export over named export.

当模块只有一个导出时,更喜欢使用默认导出而不是命名导出。

class HomePage extends Component {
  //....
}

export default HomePage

In another file :

在另一个文件中:

import HomePage from './Hello';

Check here codesandbox

在这里查看 codesandbox