Angular 2 error:

后端 未结 2 366
野趣味
野趣味 2021-01-22 20:02

Playing around with Angular 2 and trying to get this simple code working. yet I keep getting an error of:

EXCEPTION: Cannot resolve all parameters for Ta

相关标签:
2条回答
  • 2021-01-22 20:07

    You have two solutions: Inject your Tabs class globally at bootstrap:

    bootstrap(MainCmp, [ROUTER_PROVIDERS, Tabs]);
    

    On inject Tabs locally with a binding property,

    @Component({
        selector: 'tab',
        bindings: [Tabs],   // injected here
        template: `
        <ul>
          <li *ngFor="#tab of tabs" (click)="selectTab(tab)">
        [...]
    
    0 讨论(0)
  • 2021-01-22 20:11

    That's because your Tabs class is defined after your Tab class and classes in javascript aren't hoisted.

    So you have to use forwardRef to reference a not yet defined class.

    export class Tab {
        @Input() tabTitle: string;
        public active:boolean;
        constructor(@Inject(forwardRef(() => Tabs)) tabs:Tabs) {
            this.active = false;
            tabs.addTab(this);
        }
    }
    
    0 讨论(0)
提交回复
热议问题