【发布时间】:2019-06-18 01:58:52
【问题描述】:
我正在尝试创建一个单例类,这样我就可以避免再次打开同一个数据库。
我读过关于使用静态变量创建类提供程序以使其保持不变,但我发现每次导航到另一个组件时,静态变量内容都会丢失。
到目前为止,这是我的代码
couchbase.service.ts
import { Injectable } from "@angular/core";
import { Couchbase } from "nativescript-couchbase";
import * as connection from "tns-core-modules/connectivity";
import { isAndroid } from "platform";
@Injectable()
export class CouchbaseService {
private static database: any;
constructor() {
console.log("enter constructor")
}
public static init(){
if(!CouchbaseService.database) {
console.log("enter init")
CouchbaseService.database = new Couchbase("data");
}
return CouchbaseService.database;
}
public getDatabase() {
return CouchbaseService.database;
}
...
}
app.component.ts:我读过从这个父类调用init 会使应用程序为子类保留它(请注意,我还尝试将CouchbaseService 作为构造函数参数传递,更改方法@987654326 @转非静态)
import { Component } from "@angular/core";
import * as Platform from "platform";
import { CouchbaseService } from './services/couchbase.service';
@Component({
selector: "ns-app",
templateUrl: "app.component.html",
})
export class AppComponent {
constructor() {
CouchbaseService.init();
}
}
在我的app.module.ts 文件中,我将CouchbaseService 添加到providers 列表中。
import { NgModule, ErrorHandler, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
import { NativeScriptUIListViewModule } from "nativescript-ui-listview/angular";
import { NativeScriptUISideDrawerModule } from "nativescript-ui-sidedrawer/angular";
import { NativeScriptHttpModule } from "nativescript-angular/http";
import { Http } from "@angular/http";
import { NativeScriptUIDataFormModule } from "nativescript-ui-dataform/angular";
import { NativeScriptFormsModule } from "nativescript-angular/forms";
import { registerElement } from 'nativescript-angular/element-registry';
import { CardView } from 'nativescript-cardview';
import { AppRoutingModule } from "./app.routing";
import { AppComponent } from "./app.component";
import { CouchbaseService } from "./services/couchbase.service";
import { HomeComponent } from "./components/home/home.component";
registerElement('CardView', () => CardView);
registerElement("MapboxView", () => require("nativescript-mapbox").MapboxView);
registerElement("Fab", () => require("nativescript-floatingactionbutton").Fab);
@NgModule({
bootstrap: [
AppComponent
],
imports: [
NativeScriptModule,
NativeScriptUIListViewModule,
NativeScriptUISideDrawerModule,
AppRoutingModule,
NativeScriptUIDataFormModule,
NativeScriptFormsModule,
NativeScriptHttpModule
],
declarations: [
AppComponent,
HomeComponent
],
providers: [
CouchbaseService,
],
schemas: [
NO_ERRORS_SCHEMA
],
entryComponents: [
],
})
/*
Pass your application module to the bootstrapModule function located in main.ts to start your app
*/
export class AppModule { }
当我查看终端中的日志时,似乎每次我导航到另一个组件时服务都会重新启动,因此它会丢失 database 的值。
我将 NativeScript 4 与 Angular 5 和 TypeScript 2.7.2 一起使用。
【问题讨论】:
-
@Injectable({ providedIn: 'root' })将在应用程序的根目录创建一次服务。注入的每个地方都会收到相同的实例。 -
@JasonWhite
@Injectable({ providedIn: 'root' })是在 Angular 6 中引入的。用户正在使用 Angular 5。
标签: nativescript nativescript-angular