我有两个由延迟加载加载的单独模块,我试图在它们之间传递一些数据,我通过服务这样做,并且服务应该是单例的,对吗?但是当我注入服务时它会被实例化,为什么?
这是第一个组件的代码
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");}
}