In my project there are many "base" component classes. Furthermore, the project is to be divided into several other modules, in which I simply wanted to use the share module as a base classes source.
I hoped that it would be possible to use just one simple import { SharedModule } declaration in app.module and avoid many imports of base component classes in component descendants, but error occured.
(I've edited this question to be more precise.)
Example:
// base.component.ts
@Component({ selector: 'app-base', ...})
export class BaseComponent {}
// shared.module.ts
import { CommonModule } from '@angular/common';
import { BaseComponent } from './base/base.component';
@NgModule({
imports: [CommonModule],
declarations: [BaseComponent],
exports: [BaseComponent],
})
export class SharedModule {}
// app.module.ts
import { HelloComponent } from './hello.component';
import { SharedModule } from './shared/shared.module';
@NgModule({
imports: [..., SharedModule],
declarations: [AppComponent, HelloComponent],
bootstrap: [AppComponent],
})
export class AppModule {}
I know that this works (but is not my intended use):
// hello.component.ts
@Component({ template: `<app-base></app-base>` })
export class HelloComponent {}
I thought it would work too, but it doesn't:
// hello.component.ts
@Component({...})
export class HelloComponent extends BaseComponent {} // <-- Cannot find name 'BaseComponent'
Does angular allow using the base class in the above simple way somehow? (i.e. without import { BaseComponent } in each component descendant)
First of all, using a sharedModule is often an old bad practice, better to use SCAM (Single Component Angular Module) or Standalone Components.
Anyway, if you want to extend BaseComponent, you can do it, but you have to import it
import { BaseComponent } from './base/base.component';if you don't intend to use the BaseComponent itself, there is no need to declare it in any module, you just need to declare the HomeComponent