如何使用Angular将页面属性导入数据服务?

问题描述 投票:0回答:1

我想在我的Ionic应用程序中将trackingId从tracking.page.ts导入至geolocation.service.ts。有人知道如何使用Angular和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);

  }
...
javascript angular typescript ionic-framework
1个回答
0
投票

您可以提供一项服务,将trackingid保存在其中,然后将其注入第二页。

尝试在服务之外进行此操作会遇到分区和更改检测问题。

© www.soinside.com 2019 - 2024. All rights reserved.