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
?
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.
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 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.
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.
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 { }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With