Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't "export default" recommended in Angular?

According to the Typescript documentation (section "Guidance for structuring modules"):

If you’re only exporting a single class or function, use export default

Just as “exporting near the top-level” reduces friction on your module’s consumers, so does introducing a default export. If a module’s primary purpose is to house one specific export, then you should consider exporting it as a default export. This makes both importing and actually using the import a little easier.

Example:

export default class SomeType {
  constructor() { ... }
}

In the Angular documentation for components (for example) one sees this:

export class HeroListComponent implements OnInit {
  heroes: Hero[];
  selectedHero: Hero;

  constructor(private service: HeroService) { }

  ngOnInit() {
    this.heroes = this.service.getHeroes();
  }

  selectHero(hero: Hero) { this.selectedHero = hero; }
}

Generally, a component's or module's primary purpose is to house one specific export. So, is there a reason why Angular does not use or recommend using export default?

like image 255
camden_kid Avatar asked Aug 30 '17 13:08

camden_kid


People also ask

Why you should not use export default?

Refactoring. Default exports make large-scale refactoring impossible since each importing site can name default import differently (including typos). On the contrary, named exports make such refactoring trivial.

What is export default in angular?

If you're only exporting a single class or function, use export default. Just as “exporting near the top-level” reduces friction on your module's consumers, so does introducing a default export. If a module's primary purpose is to house one specific export, then you should consider exporting it as a default export.

Does not provide an export named default?

This is the reason the error occurs. To solve the error "The requested module does not provide an export named 'default'", use the default keyword when exporting a value from a file and don't wrap the corresponding import in curly braces. You can only have a single default export per file.

Why do we use export in angular?

An export what you put is the exports property of the @NgModule decorator. It enables an Angular module to expose some of its components/directives/pipes to the other modules in the applications. Without it, the components/directives/pipes defined in a module could only be used in that module.


1 Answers

The actual reason is that this doesn't work with the AOT compiler, however it will work with the JIT compiler. So if you are using AOT (or want to use it moving forward), don't do export default. See also here:

Default Exports

Default exports aren't allowed with AoT - they must all be named.

❌ DONT:

import { Component } from '@angular/core';

@Component({
  template: `
    <div class="example">
      Example component!
    </div>
  `
})
export default class ExampleComponent {

}

✅ DO:

import { Component } from '@angular/core';

@Component({
  template: `
    <div class="example">
      Example component!
    </div>
  `
})
export class ExampleComponent {

}
like image 170
Andrei Matracaru Avatar answered Sep 17 '22 11:09

Andrei Matracaru