다음 코드로 서비스를 만들었습니다. 나는 IP와 포트로 네이티브 스토리지를 생성하기위한 두 가지 함수를 작성했으며 다음은 네이티브 스토리지에서 값을 가져 오는 것입니다.서비스에서 Ionic 2의 페이지로 값 전달
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.
}
});
}
...
}
나는 네이티브 스토리지를 만들 수 있었고, 또한 네이티브 스토리지에서 값을 검색하지만 난에서 그것을 통과하지 수 내 서비스를 설정 페이지로 보냅니다. 제발 조언. 고맙습니다. 나는 아직도 이것을 풀려고 노력하고있다. 도와주세요. 당신이 약속을 반환하는 방법을 약간 조정할 경우
내 이온 정보는
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
당신이'service.ip'에 접근하려 할 때, 테스트, 설정 페이지 코드 변경, 제한 시간 안에 콘솔 로그 감싸기, 이렇게 정의 된 것 같습니다 :'setTimeout (() = > {console.log (this.service.ip + "IP from the Service");}, 100)'그리고 귀하의 IP 주소가 기록되어 있는지 확인하십시오 –
Thanks @FabioAntunes. 예, 작동하지만 setTimeout() 함수를 사용하고 싶지 않습니다. 다른 방법이 있습니까? –
그리고 때때로 작동하는 경우도 있습니다. –