我有两个由延迟加载加载的独立模块,我试图在它们之间传递一些数据,而这由服务执行,服务应该是单例吧?但是当我注入服务时,为什么会实例化?
这是第一个组件的代码
import { Component} from '@angular/core';
import { LocationService } from '../services/location.service';
import {Location} from '../shared/location';
import { NgForm } from '@angular/forms';
@Component({
selector: 'app-about',
templateUrl: 'about.page.html',
styleUrls: ['about.page.scss']
})
export class AboutPage {
country :string;
city :string;
constructor(public locationService: LocationService){ }
locationSelected(){
let newLocation = {country: this.country,city: this.city};
this.locationService.location = newLocation;
console.log(this.locationService.location);
}
}
这是第二个组成部分
import { Component, OnInit, Input } from '@angular/core';
import {LocationService} from '../Services/location.service';
import { PrayerTimeService } from '../services/prayer-time.service';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss']
})
export class HomePage implements OnInit {
@Input()
prayerTime = {
Asr : "15:48 (AST)",
Dhuhr : "12:29 (AST)",
Fajr : "04:36 (AST)",
Isha : "20:23 (AST)",
Maghrib : "19:02 (AST)",
Sunrise : "05:56 (AST)"
}
constructor(private prayerTimeService: PrayerTimeService, public locationService : LocationService){}
ngOnInit(){
console.log(this.locationService.location);
this.getPrayerTimes();
}
getPrayerTimes(){
this.prayerTimeService.getTimes(this.locationService.location.country, this.locationService.location.city).subscribe((data) => {
this.prayerTime = data.data[0].timings;
console.log(this.prayerTime);
});
}
}
以及提供服务的父模块
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule, RouteReuseStrategy } from '@angular/router';
import { HttpClientModule } from '@angular/common/http';
import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { LocationService } from './Services/location.service';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import {FormsModule} from '@angular/forms';
import {AboutPageModule} from './about/about.module';
@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule, HttpClientModule, FormsModule, AboutPageModule],
providers: [
StatusBar,
SplashScreen,
LocationService,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule {}
这是服务
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Location } from '../Shared/location';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class LocationService {
location : Location = new Location("","");
constructor(private http : HttpClient) { console.log("new Service has Been created");}
}
答案 0 :(得分:0)
该服务没有恢复原状。问题在于在ngOnInit中对服务的调用发生在调用locationSelected()方法之前。因此,此位置不会在此后不久初始化。