2

I know what's the difference between export class xxx and export default class xxx.

But I saw many angular4 project use export class xxx like this:

@Component({
  selector: 'call-record',
  templateUrl: './collection.component.html',
  styleUrls: ['./collection.component.css']
})
export class collection {

}

I think if there is one class or whatever to export, use export default xxx should be better?

So I can import it like this: import xxx from './somewhere'

not this: import {xxx} from './somewhere'.

Lin Du
  • 88,126
  • 95
  • 281
  • 483
  • https://stackoverflow.com/a/33307487/2545680 – Max Koretskyi Jul 27 '17 at 13:18
  • There's also some issues around using `default` keyword when using with angular-cli, see https://github.com/angular/angular-cli/issues/3826 – penleychan Jul 27 '17 at 13:48
  • Possible duplicate of [Typescript export vs. default export](https://stackoverflow.com/questions/33305954/typescript-export-vs-default-export) – LarsMonty Jul 27 '17 at 16:17
  • @LarsMonty @Maximus I don't think my question is duplicated. As I said, I __know__ what's the difference between `export xxx` and `export default xxx`. My question's point is why so many projects of angular4 use `export xxx` not `export default xxx` – Lin Du Jul 28 '17 at 06:07

1 Answers1

0

There are some problems with maintainability:

  • If you refactor xxx in the somewhere module, it will not be renamed in the file that imports the module.

  • If you eventually need to export more useful information(not only concrete class but also types, interfaces, constants) from the file then you must use export { ConcreteClass, SomeType, ISomeInterface, SomeConstant}.

  • The discoverability of the default export is very poor, and you cannot intelligently distinguish whether a module has a default export

  • Editor/IDE automatic completion

  • If you use CommonJs like this const {default} = require('./somewhere'), you most likely want to rename the default export.

  • Prevent spelling errors. If you use import Foo from './foo' during development, you won't get any spelling errors. Others might write import foo from'./foo' like this.

For the above reasons, the default export should be avoided.

Lin Du
  • 88,126
  • 95
  • 281
  • 483