如何将状态从登录页面传递到ionic中的app.component [英] How to pass status from login page to app.component in ionic

查看:49
本文介绍了如何将状态从登录页面传递到ionic中的app.component的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将登录状态从登录文件传递给ionic中的app.component文件,其中我的登录文件位于pages文件夹中.

I want to pass login status from login file to app.component file in ionic where my login file in pages folder.

login.ts

this.http.post('http://localhost:80',{
      "username": email,
      "password": password
      }).subscribe((data: any) => {
    if(data.status === 'success'){
      this.navCtrl.navigateRoot('/home');
}

如上面的代码所示,我想将此状态从登录文件传递到app.component文件

as shown in above code i want to pass this status from login file to app.component file

推荐答案

离子事件将帮助您实现这一目标.您可以使用数据在 login.ts 中发布事件,并将其订阅到 app.component.ts 以获取数据,我的意思是这样的状态:

Ionic Events will helps you to achieve this. You can publish event in your login.ts with the data and subscribe it to app.component.ts to get data i means whatever your status like this :

login.ts

import { Events } from 'ionic-angular';
...
...
constructor(public events: Events) {}

yourMethod() {
    this.http.post('http://localhost:80',{
        "username": email,
        "password": password
    }).subscribe((data: any) => {
        if(data.status === 'success'){
            this.navCtrl.navigateRoot('/home');
            this.events.publish('eventName', data);
        }
    }
}

app.component.ts

import { Events } from 'ionic-angular';
...
...
constructor(public events: Events) {
    events.subscribe('eventName', (data) => {
        // data is the same that your have passed when you published the event
        console.log('data', JSON.stringify(data));
    });
}


这篇关于如何将状态从登录页面传递到ionic中的app.component的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆