Class is using Angular features but is not decorated. Please add an explicit Angular decorator

前端 未结 3 2316
暖寄归人
暖寄归人 2020-12-20 11:36

I have some components like CricketComponent, FootballComponent, TennisComponent etc. All These Classes have some common properties :-

相关标签:
3条回答
  • 2020-12-20 12:12

    While the accepted answer is correct in using @Component, one minor downside is that it requires all constructor argument types to be resolvable by DI. So if your base class constructor takes a constant/literal - say, an enum - as a flag, you're going to have to set up a corresponding DI provider/token just to get it to compile.

    You can however also resolve NG2007 using the @Directive decorator instead, which doesn't require DI compatibility

    0 讨论(0)
  • 2020-12-20 12:21

    With Angular 10, you can fix this issue by adding the decorator @Injectable() to your abstract base class like this:

    @Injectable()
    export abstract class BaseComponent {    
        @Input() teamName: string;
        @Input() teamSize: number;
        @Input() players: any;
    }
    
    0 讨论(0)
  • 2020-12-20 12:29

    You'll need to add a @Component decorator to that base class (which should probably also be declared abstract).

    This is the bare minimum you can get away with:

    @Component({
      template: ''
    })
    export abstract class BaseComponent {
    
        @Input() teamName: string;
        @Input() teamSize: number;
        @Input() players: any;
    }
    
    0 讨论(0)
提交回复
热议问题