我想在我的Ionic应用程序中将trackingId从tracking.page.ts导入到geolocation.service.ts。有人知道如何用角和TypeScript来实现这个目的吗?
下面您可以看到代码的摘录。
tracking.page.ts
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { Geolocation } from '@ionic-native/geolocation/ngx';
import { GeolocationService } from '../../app/geolocation.service';
import { UserService } from '../../app/user.service';
import { Insomnia } from '@ionic-native/insomnia/ngx';
import { LoadingController } from '@ionic/angular';
import { AlertController } from '@ionic/angular';
import { AppComponent } from '../app.component';
import { Storage } from '@ionic/storage';
declare var google;
@Component({
selector: 'app-tracking',
templateUrl: './tracking.page.html',
styleUrls: ['./tracking.page.scss'],
})
export class TrackingPage implements OnInit {
@ViewChild('map', { static: true }) mapElement: ElementRef;
map: any;
markers = [];
geoLocations: any;
watchLocationUpdates: any;
isWatching: boolean;
interval: any;
geoLatitude: number;
geoLongitude: number;
geoAccuracy: number;
timeStamp: any;
uId: string;
trackingId: string; // This is the trackingId
...geolocation.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment } from '../environments/environment';
@Injectable({
providedIn: 'root'
})
export class GeolocationService {
databaseUrl = environment.firebase.databaseURL;
constructor(private http: HttpClient
) {
console.log('Hello GeolocationService Provider');
console.log('GeolocationService: ', this.databaseUrl);
}
...发布于 2020-03-01 21:46:07
您可以创建一个服务,将trackingid保存在其中,然后将其注入第二个页面中。
尝试在服务之外执行此操作将遇到分区和更改检测方面的问题。
发布于 2020-03-03 12:01:35
使用可观察的主题在服务文件中定义trackingid,如
public trackingid: BehaviorSubject<number> = new BehaviorSubject(1);,然后将其设置为服务内部,并按如下方式获取它
public getTrackingId(): boolean {
return this.trackingid.value;
}
public setTrackingId(){
return this.trackingid.next(2);
}现在,您可以从组件中获取并设置为trackingId。
https://stackoverflow.com/questions/60479922
复制相似问题