Reputation: 1038
Anyone knows how to create an angular2 service with Ionic2 v2.1.0 ?
Already tried a lot of things but keep getting errors.
I followed some answers from here How to use angular 2 service with Ionic 2 but it doesn't work...
Here is my code
DataBaseService.ts
import {Injectable} from "@angular/core";
@Injectable()
export class DataBaseService {
constructor() {
}
execSQL() {
console.log('call works');
}
}
Imported in "app.component.ts"
import { DataBaseService } from 'DataBaseService';
And put it in the @Component of "app.component.ts"
@Component({
templateUrl: 'app.html',
providers: [DataBaseService]
})
If I run the app this way, I get the following error
Uncaught Error: Cannot find module "DataBaseService"
Upvotes: 1
Views: 6166
Reputation: 2915
regarding https://angular.io/styleguide add providers to NgModule and them in component constructor. services should be initialized when app bootstraps.
@NgModule({
imports: [MyModules]
declerations: [MyComponent]
providers: [MyService]
});
Upvotes: 1
Reputation: 29614
If the service file is in the same location do
import { DataBaseService } from './DataBaseService';
You are getting the error because it is looking for a module when you give it directly as import { DataBaseService } from 'DataBaseService';
Upvotes: 1