将服务注入选项卡ionic 2

马克西米利亚诺·莫拉莱斯(Maximilian Morales)

我正在关注ionic 2中的本教程高级google maps组件
我一步一步地完成了它,并且完美地运行了,但仅在一页纸中。在我的项目中,我计划使用选项卡或侧面菜单设计。
我已经在本教程的博客上询问过,但仍然没有任何回复。
在此先感谢您的帮助或建议。
我尝试了以下操作,但在控制台中没有错误。
map.js:

import {Page, NavController} from 'ionic-angular';
import {ConnectivityService} from '../../providers/connectivity-service/connectivity-service';
@Page({
  templateUrl: 'build/pages/map/map.html',
  providers: [ConnectivityService],
})
export class MapPage {
  static get parameters() {
    return [[NavController],[ConnectivityService]];
  }
  constructor(nav, connectivityService) {
    this.nav = nav;
    this.connectivity = connectivityService;
    this.map = null;
    this.mapInitialised = false;
    this.apiKey = null;
    this.loadGoogleMaps();
  }
  loadGoogleMaps(){
    var me = this;
    this.addConnectivityListeners();
    if(typeof google == "undefined" || typeof google.maps == "undefined"){
        console.log("Google maps JavaScript needs to be loaded.");
        this.disableMap();
        if(this.connectivity.isOnline()){
            console.log("online, loading map");
            window.mapInit = function(){
                me.initMap();
                me.enableMap();
            }
            let script = document.createElement("script");
            script.id = "googleMaps";
            if(this.apiKey){
                script.src = 'http://maps.google.com/maps/api/js?key=' + apiKey + '&callback=mapInit';
            } else {
                script.src = 'http://maps.google.com/maps/api/js?callback=mapInit';               
            }
            document.body.appendChild(script);  
        }   
    }
    else {
        if(this.connectivity.isOnline()){
            console.log("showing map");
            this.initMap();
            this.enableMap();
        }
        else {
            console.log("disabling map");
            this.disableMap();
        }
    }
  }
  initMap(){
    this.mapInitialised = true;
    navigator.geolocation.getCurrentPosition( (position) => {
        let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        let mapOptions = {
          center: latLng,
          zoom: 15,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        this.map = new google.maps.Map(document.getElementById("map"), mapOptions);
    }, (error) => {
        console.log(error);
    });
  }
  disableMap(){
    console.log("disable map");
  }
  enableMap(){
    console.log("enable map");
  }
  addConnectivityListeners(){
    var me = this;
    var onOnline = function(){
        setTimeout(function(){
            if(typeof google == "undefined" || typeof google.maps == "undefined"){
                me.loadGoogleMaps();
            } else {
                if(!me.mapInitialised){
                    me.initMap();
                }
                me.enableMap();
            }
        }, 2000);
    };
    var onOffline = function(){
        me.disableMap();
    };
    document.addEventListener('online', onOnline, false);
    document.addEventListener('offline', onOffline, false);
  }
}`


Connectivity-service.js:

import {Injectable} from 'angular2/core';
import {Platform} from 'ionic-angular';

@Injectable()
export class ConnectivityService {
  static get parameters(){
    return [[Platform]]
  }  

  constructor(platform) {
    this.platform = platform;
    this.onDevice = this.platform.is('ios') || this.platform.is('android');
  }

  isOnline() {
    if(this.onDevice && navigator.connection){
      let networkState = navigator.connection.type;
      return networkState !== Connection.NONE;
    } else {
      return navigator.onLine;      
    }
  }

  isOffline(){
    if(this.onDevice && navigator.connection){
      let networkState = navigator.connection.type;
      return networkState === Connection.NONE;
    } else {
      return !navigator.onLine;     
    }
  }
}


app.js

import {App, Platform} from 'ionic-angular';
import {StatusBar} from 'ionic-native';
import {TabsPage} from './pages/tabs/tabs';


@App({
  template: '<ion-nav [root]="rootPage"></ion-nav>',
  config: {} // http://ionicframework.com/docs/v2/api/config/Config/
})
export class MyApp {
  static get parameters() {
    return [[Platform]];
  }

  constructor(platform) {
    this.rootPage = TabsPage;

    platform.ready().then(() => {
      StatusBar.styleDefault();
    });
  }
}


tabs.html:

<ion-tabs>
  <ion-tab [root]="mapPage" tabIcon="map"></ion-tab>
  <ion-tab [root]="listPage" tabIcon="list-box"></ion-tab>
</ion-tabs>


tabs.js

import {Page} from 'ionic-angular';
import {MapPage} from '../map/map';
import {ListadoPage} from '../listado/listado';

@Page({
  templateUrl: 'build/pages/tabs/tabs.html',
})
export class TabsPage {
  constructor() {
    this.mapPage = MapPage;
    this.listPage = ListadoPage;
  }
}
曼努·瓦尔德斯(Manu Valdes)

您具有正确的体系结构,它也可以与菜单或选项卡或许多不同的页面一起使用;侧面菜单或标签只是一种显示页面的方式。

如果您担心自己从菜单或选项卡模板访问服务,请记住它们只是@App该类的关联模板因此,要从他们那里访问服务,您需要将服务添加到一样将其添加到的constructor(您现在没有这样做)。@App@Page

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Ionic 2中将数据从子选项卡传递到父选项卡

Ionic 2将选项卡NavParams传递给选项卡

仅将选项卡中的参数发送到其他页面 - Ionic2

在ionic2中设置默认选项卡

Ionic 2-Sidemenu中的“注销”选项卡

Ionic 2-菜单中的选项卡导航

使用angular2或打字稿输入选项卡A中的字段后,如何在选项卡B中预填充字段

Ionic2,将NavController注入可注入服务

Ionic 2 设备后退按钮防止应用程序在选项卡中退出

带有内容滑动的 Ionic2 可滑动选项卡

如何正确隐藏仅用于登录页面的ionic2选项卡?

ionic 2中多个选项卡中的单个搜索栏过滤器项

CRUD操作后更改选定的选项卡-Ionic2

如何在ionic 2中选择选项卡?

所有页面(包括新页面)中的ionic 2选项卡

我们如何在ionic 2应用程序中显示多个选项卡?

在Ionic 4中无法使选项卡作为2页工作

在Ionic 2中,如何在单击另一个选项卡菜单应用程序图标时隐藏一个选项卡?

如何从ionic v2的父选项卡组件中弹出一个选项卡的历史记录

Angular 2:在选项卡更改时将事件发送给子级

选中状态后,将2列数据移至其他选项卡

ConEmu:如何将msys2称为选项卡?

将Console2指向Cygwin时没有选项卡

注销后的Ionic2选项卡仍显示在导航页面中,我在这里错过了什么?

Ionic2-每当我使用navCtrl推送新页面/组件时,选项卡就会消失

离子2 +角度2-选项卡+侧面菜单

从选项卡1中选择ListView后切换到选项卡2

Bootstrap:如何通过单击单个选项卡来显示2个选项卡内容?

ec2上的RStudio服务器-关闭浏览器选项卡时不持久