如何从另一个ts文件更新component.ts文件中的vaiable

Moh*_*opi 1 ionic2 ionic3 angular

最初我的app.component.ts文件被加载然后我有两个变量,nameemail从API获取调用更新,为了进行此api调用我需要用户名如果​​我没有用户名我将无法更新此名称和电子邮件.

一旦我获得了用户名,我就可以拨打api电话,但我的用户名只有在登录成功后才可用.

我的app.html有这个,下面的代码是我的sidemenu的一部分

<ion-item class="profile-info">
  <ion-icon name="person" item-left></ion-icon>
  <h2>{{name}}</h2>
  <p class="se-email">{{email}}</p>
</ion-item>
Run Code Online (Sandbox Code Playgroud)

我的app.component.ts文件有这个

name 
 email

 if(localStorage.getItem("username")){
   //here it is invoking 2nd time because i have stored the username after login so no problem
    this.getUserDetails();//this function call the same api 
  }else{
    //at first thime i am not able update the name and email because i dont have username 
    //so i am trying to update the name with set and get method where it will return my data 
    this.name =this.service.getUserName();
    this.email = this.service.getUserEmail();
  }
Run Code Online (Sandbox Code Playgroud)

问题

我无法从主页或登录页面更新名称和电子邮件变量

如何从home.ts或login.ts等其他页面更新我的app.component.ts文件

更新:

我完整的app.html页面

<ion-menu [content]="content">
  <ion-header id="slidermenu_header">
    <ion-toolbar>
      <ion-title style="padding: 0 8px 4px;">SEcure Me</ion-title>
    </ion-toolbar>
  </ion-header>

  <ion-content class="slidermenu_content">
    <ion-list style="margin: -1px 0 24px -5px;">
      <ion-item class="profile-info">
          <ion-icon name="person" item-left></ion-icon>
          <h2 >{{name}}</h2>
          <p class="se-email">{{email}}</p>
      </ion-item>
    </ion-list>
    <div id="slidermenulist">
      <ion-item *ngFor="let p of pages" (click)="openPage(p)" menuClose>
        <ion-icon [name]="p.icon" item-left></ion-icon>
          {{p.title}}
      </ion-item>
    </div>
  </ion-content>
</ion-menu>

<!-- Disable swipe-to-go-back because it's poor UX to combine STGB with side menus -->
<ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>
Run Code Online (Sandbox Code Playgroud)

从上面的代码中你可以看到我在我的应用程序中只使用一个侧面菜单但是如果我在许多页面上使用这个侧面菜单

如果我想更新我的名字和电子邮件,我必须去我的app.component.ts文件,如this.name="username"' andthis.email ="email@gmail.com"`工作正常

以同样的方式,如果我在我的home.ts文件中给出姓名和电子邮件,我无法看到任何内容

Str*_*ker 7

在app.component.ts中:

import { Events } from 'ionic-angular';
constructor(public events: Events) {
   events.subscribe('user:login', () => {
     this.loggedIn();
   });
}

loggedIn() {
  console.log("logged in");
}
Run Code Online (Sandbox Code Playgroud)

在调用页面中:

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

logIn() {
  events.publish('user:login');
}
Run Code Online (Sandbox Code Playgroud)