nativescript and angular2 - no Provider Error

377 views Asked by At

I've got problem with loading nativescript app using angular2 and typescript. I'm trying to solve for long time. What i have is:

list.service.ts

import { ItemListApi } from '../sdk/services/custom/ItemList';
import { ItemList } from '../sdk/models/ItemList';

@Injectable()
export class ListService {
items: Observable<Array<Item>>;
public grocery: Grocery = new Grocery('', '');
private listModel: ItemList = new ItemList();

constructor(private itemListApi: ItemListApi,
    private authModelApi: AuthModelApi,
    private loginCredentials: LoginCredentials) {

}

addItems(list: ItemList) {
    let uid;
       uid = this.loginCredentials.getUserId();
         return this.itemListApi.create({
        "name": list.name,
        "userid": uid
    });
};
}

module.ts

@NgModule({
imports: [
CommonModule,
FormsModule,
RouterModule
],
declarations: [
LoginComponent,
ListComponent
],
providers: [
LoginService,
ListService,
LoginCredentials,
HeroActions,
HeroService
],
exports: [
LoginComponent,
ListComponent
]
})

All the time I'm getting error message saying "No provider for ItemListApi" ... actually ItemListApi is from my loopback nativescript sdk. can someone help me spot that small issue with my code?

2

There are 2 answers

2
mast3rd3mon On

Add ItemListApi into your app.module.ts's provider list:

providers: [
LoginService,
ListService,
LoginCredentials,
HeroActions,
HeroService,
ItemListApi
],

In reply to what Nick said, he cant add it as a provider in the ts file he's in as he is using @Injectable not @Component

0
Undrium On

As mentioned in the comments; you need to update your imports in the module.ts file.

Add:

import { SDKBrowserModule } from './shared/sdk/index';

with the other imports in your module.ts, then also:

SDKBrowserModule.forRoot()

in your imports array in module.ts