Prefer default export on a file with single export
时间: 2023-10-31 20:56:37 浏览: 350
Yes, when there is only a single export in a file, it is preferable to use a default export. This allows for a simpler and cleaner import syntax when using the exported module. Instead of having to use curly braces and specify the name of the export, you can directly import it using any name you prefer.
For example, consider a file named "utils.js" with a single export:
```javascript
// utils.js
const add = (a, b) => a + b;
export default add;
```
In another file, you can import the default export like this:
```javascript
// app.js
import add from './utils.js';
console.log(add(2, 3)); // Output: 5
```
Using default exports in such cases reduces the verbosity of the code and provides a more concise syntax.
阅读全文