简体   繁体   English

将值从服务传递到 Ionic 2 中的页面

[英]Passing value from a service to a page in Ionic 2

I created a service with the following code.我使用以下代码创建了一个服务。 I wrote two functions one for creating the native storage with ip and port and the next one is for getting the values from the native storage.我写了两个函数,一个是用 ip 和 port 创建本机存储,另一个是从本机存储中获取值。

DatabaseService数据库服务

export class DatabaseService {
  ...
  public ip: string;
  public port: string;
  ...
  public createConnectionInfo(ip: string, port: string) {
    NativeStorage.setItem('connectionStorage', { ip: ip, port: port })
      .then(
        () => console.log('Stored Connection Information!'), //Working
        error => console.error('Error storing connection information', error)
      );
  }
  ...
  public getConnectionInfo() {
    this.platform.ready().then(() => {
      NativeStorage.getItem('connectionStorage').then(data => {
        this.ip = data.ip; // I'm setting the ip in this public variable
        this.port = data.port;
        console.log(this.ip); //Works
      }, error => {
        this.navCtrl.push(SettingPage);
      });
    });
  }
  ...
}

SettingPage设置页面

export class SettingPage {
  connection: any;
  service: DatabaseService;
  ip: string;
  port: string;
  constructor(public navCtrl: NavController, platform: Platform, service: DatabaseService) {
    this.service = service;
    platform.ready().then(() => {
      if(true){ 
        console.log(this.service.ip+" IP FROM THE SERVICE"); \\I'm trying to get the ip from the service.
      }
    });
  }
  ...
}

I was able to create the native storage and also retrieved the values from the native storage but i couldn't pass that from my service to the setting page.我能够创建本机存储并从本机存储中检索值,但我无法将其从我的服务传递到设置页面。 Please advise.请指教。 Thank you.谢谢你。 I'm still trying to get this solved.我仍在努力解决这个问题。 Please help.请帮忙。

My Ionic Info我的离子信息

Cordova CLI: 6.3.1                                                                                                              
Gulp version:  CLI version 3.9.1                                                                                                
Gulp local:                                                                                                                     
Ionic Framework Version: 2.0.0-rc.1                                                                                             
Ionic CLI Version: 2.1.0                                                                                                        
Ionic App Lib Version: 2.1.0-beta.1                                                                                             
ios-deploy version: Not installed                                                                                               
ios-sim version: Not installed                                                                                                  
OS: Mac OS X El Capitan                                                                                                         
Node Version: v6.7.0                                                                                                            
Xcode version: Xcode 7.3.1 Build version 7D1014

You could use the getConnectionInfo method, to get your ip address if you tweak that method a bit to return a promise.如果您稍微调整该方法以返回承诺,您可以使用getConnectionInfo方法来获取您的 IP 地址。 Something like this:像这样的东西:

public getConnectionInfo() {
  return this.platform.ready().then(() => NativeStorage.getItem('connectionStorage'))
    .then(data => {
      this.ip = data.ip;
      this.port = data.port;
      console.log(this.ip);
      return data;
    }).catch(error => {
      this.navCtrl.push(SettingPage);
      return error;
    });
  });
}

Then on your SettingPage instead of accessing directly the value you would call the getConnectionInfo , like this:然后在您的SettingPage 上,而不是直接访问您将调用getConnectionInfo的值,如下所示:

export class SettingPage {
  connection: any;
  service: DatabaseService;
  ip: string;
  port: string;
  constructor(public navCtrl: NavController, platform: Platform, service: DatabaseService) {
    this.service = service;
    platform.ready().then(() => this.service.getConnectionInfo())
      .then(() =>{
        if(true){ 
          console.log(this.service.ip+" IP FROM THE SERVICE");
        }
    });
  }
  ...
}

You might need to do some tweaking to this code, but it should work as it is.您可能需要对此代码进行一些调整,但它应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM