任何人都知道如何使用Ionic2 v2.1.0创建angular2服务?
已经尝试过很多东西,但不断出错。
我从How to use angular 2 service with Ionic 2跟随了一些答案,但它不起作用......
这是我的代码
DataBaseService.ts
import {Injectable} from "@angular/core";
@Injectable()
export class DataBaseService {
constructor() {
}
execSQL() {
console.log('call works');
}
}
导入“app.component.ts”
import { DataBaseService } from 'DataBaseService';
并将其放在@ apponent of“app.component.ts”
中@Component({
templateUrl: 'app.html',
providers: [DataBaseService]
})
如果我以这种方式运行应用程序,我会收到以下错误
Uncaught Error: Cannot find module "DataBaseService"
答案 0 :(得分:1)
如果服务文件位于同一位置,请执行
import { DataBaseService } from './DataBaseService';
您收到错误是因为当您直接将其作为import { DataBaseService } from 'DataBaseService';
答案 1 :(得分:1)
关于https://angular.io/styleguide向NgModule添加提供程序以及它们在组件构造函数中。应用程序启动时应初始化服务。
@NgModule({
imports: [MyModules]
declerations: [MyComponent]
providers: [MyService]
});