Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ionic framework 如何始终检查Ionic 2中是否启用了位置服务?_Ionic Framework_Ionic2 - Fatal编程技术网

Ionic framework 如何始终检查Ionic 2中是否启用了位置服务?

Ionic framework 如何始终检查Ionic 2中是否启用了位置服务?,ionic-framework,ionic2,Ionic Framework,Ionic2,我正试图找出如何始终检查位置服务是否已启用。我的意思是像一个实时检查器。我现在只有一种观点。我正在检查用户何时登录-如果位置服务已启用,他将登录。但是,如果未启用,则会出现一个警报对话框: 这是我的函数,用于检查是否已启用: checkLocation() { this.diagnostic.isLocationEnabled().then( (isAvailable) => { console.log('Is available? ' + isAva

我正试图找出如何始终检查位置服务是否已启用。我的意思是像一个实时检查器。我现在只有一种观点。我正在检查用户何时登录-如果位置服务已启用,他将登录。但是,如果未启用,则会出现一个警报对话框:

这是我的函数,用于检查是否已启用:

 checkLocation() {
    this.diagnostic.isLocationEnabled().then(
      (isAvailable) => {
        console.log('Is available? ' + isAvailable);
        if (isAvailable) {
          this.navCtrl.setRoot(UserTypePage);
        } else {
          alert('Please turn on the location service');
          if (this.autoLogin) {
            this.autoLogin();
          }
        }
      }).catch((e) => {
        console.log(e);
        alert(JSON.stringify(e));
      });
  }
当用户尝试登录时,我调用此函数

Facebook登录示例:

facebookLogin(): void {
    this.global = ShareService.getInstance();
    this.subscriptions.push(this.authProvider.loginWithFacebook().subscribe((user) => {
      this.loading.dismiss().then(() => {

        this.global.setUserName(user.displayName); 
        this.global.setProfilePicture(user.photoURL);
        this.global.setUserId(this.authProvider.currentUserId);
        this.tokenstore();
        this.checkLocation(); //HERE
      })

    }, error => {
      this.loading.dismiss().then(() => {
        let alert = this.alertCtrl.create({
          message: error.message,
          buttons: [
            {
              text: "Ok",
              role: 'cancel'
            }
          ]
        });
        alert.present();
      });
    }, (err) => {
      console.error("error: " + JSON.stringify(err));
    }));
    this.loading = this.loadingCtrl.create({
      content: 'Signing in...'
    });
    this.loading.present();
  }

我希望这个功能在整个应用程序中工作,而不仅仅是在登录视图中。如何做到这一点?

这是一个经典场景,角度依赖注入将帮助您跨组件/视图重用现有方法

您可以在应用程序中创建LocationCommonService,并定义检查位置服务是否已启用的方法

现在在需要调用所需函数的所有组件中注入LocationCommonService