Øle Bjarnstroem Øle Bjarnstroem - 1 month ago 29
Javascript Question

Angular 2 Error: (SystemJS) Can't resolve all parameters for Member: (?).(…)

I use this seed application to narrow down an error that keeps popping up (so debugging is easier...)

I keep getting this error when trying to add a data model to my shared module (in my browser console):

Error: (SystemJS) Can't resolve all parameters for Member: (?).(…)


The
Member Class
in question:

import * as _ from 'lodash';
import { Injectable } from '@angular/core';

@Injectable()
export class Member {

private id: string;
[key: string]: any;

constructor(private data?: any) {
if (data) {
this.id = data.id;
_.extend(this, data.attributes);
}
}

}


My
SharedModule
(the
Member Class
isn't referenced anywhere else for now):

import { NgModule, ModuleWithProviders } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router';

import { ToolbarComponent } from './toolbar/index';
import { NavbarComponent } from './navbar/index';
import { NameListService } from './name-list/index';

import { Member } from './models/member.model';

@NgModule({
imports: [CommonModule, RouterModule],
declarations: [ToolbarComponent, NavbarComponent],
exports: [ToolbarComponent, NavbarComponent,
CommonModule, FormsModule, RouterModule]
})
export class SharedModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: SharedModule,
providers: [NameListService, Member]
};
}
}


When I get rid of the constructor in
class Member
the error disappears:

import * as _ from 'lodash';
import { Injectable } from '@angular/core';

@Injectable()
export class Member {

private id: string;
[key: string]: any;

}


I am not using barrel imports as you can see since the order of the imports can cause the same error.

I am a bit stuck on how to solve this... Thanks

Answer

If the class is just to be used as a model, then don't add it to the @NgModule.providers and don't try to inject it. Just import the class into the class file where you need it, and just use it like you would any other normal class

import { Member } from './member.model';

@Component({})
class MyComponent {
  member = new Member();
}

See Also:

Comments