如何更新离子2侧菜单中的值

Vov*_*per 10 javascript typescript ionic2 ionic3 angular

如何将日期从页面"转移"到side-menuIonic2中,即 - 具有app.page.html以下内容:

<ion-menu [content]="content">

    <ion-content class="menu-left">
        <!-- User profile -->
        <div text-center padding-top padding-bottom class="primary-bg menu-left">
            <a menuClose>
                <h4 light>{{userName}}</h4>
            </a>
        </div>

        <ion-list class="list-full-border">
            <button ion-item menuClose *ngFor="let page of pages" (click)="openPage(page)">
        <ion-icon item-left name="{{ page.icon }}"></ion-icon>
        {{ page.title }}
        <ion-badge color="danger" item-right *ngIf="page.count">{{ page.count }}</ion-badge>
      </button>
        </ion-list>
    </ion-content>

</ion-menu>

<ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>
Run Code Online (Sandbox Code Playgroud)

如何userName根据数据从页面中的操作更新值facebookLogin.page

facebookLogin.page.ts:

...
fbLogin() {
    Facebook.login(["public_profile", "email"])
      .then((resp: FacebookLoginResponse) => {

        if (resp.status === "connected") {
          Facebook.api("/me", ["public_profile", "email"])
            .then(res => {
             this.userName = res.name
              this.login({name: this.userName})
            })
        ...
      );
  }

 login(params) {
    this.nav.setRoot(HomePage, params);
  }
...
Run Code Online (Sandbox Code Playgroud)

(所以,我将如何导入userName我的登录与Facebook入后得到ion-sideMenuapp.html,我怎么可能让EventEmmiter,服务/遵守吗?在app.html的离子菜单...一些其他的方式的变化)

Iva*_*ers 15

ionic2 - 使用事件

查看活动文档

它们允许您从任何页面"发布"操作,并在另一个页面中订阅它以检索该值.你的场景看起来有点像这样.

导入(打开Facebooklogin.componentapp.component)

import { Events } from 'ionic-angular'; 并在您的构造函数中 constructor(public events: Events)

然后,每当你改变你的userName(Facebook登录处理程序中的fe)时,发布这样的值.

fbLogin() {
    Facebook.login(["public_profile", "email"])
      .then((resp: FacebookLoginResponse) => {

        if (resp.status === "connected") {
          Facebook.api("/me", ["public_profile", "email"])
            .then(res => {
             this.userName = res.name
             // publish the username change to the events
             this.events.publish('username:changed', this.userName);
              this.login({name: this.userName})
            })
        //...
      );
  }
Run Code Online (Sandbox Code Playgroud)

并订阅任何出版物 app.component

userName: string;

constructor(events: Events) {
   this.userName = "not logged in";

   events.subscribe('username:changed', username => {
      if(username !== undefined && username !== ""){
        this.userName = username;
      }
   }) //... 
}
Run Code Online (Sandbox Code Playgroud)

angular2 - 使用EventEmitter

import { EventEmitter } from '@angular/core';

public userChanged = new EventEmitter();

fbLogin() {
        Facebook.login(["public_profile", "email"])
          .then((resp: FacebookLoginResponse) => {

            if (resp.status === "connected") {
              Facebook.api("/me", ["public_profile", "email"])
                .then(res => {
                 this.userName = res.name
                 // publish the username change to the events
                 this.userChanged.emit(this.userName);
                 this.login({name: this.userName})
                })
            ...
          );
      }
Run Code Online (Sandbox Code Playgroud)

App.component

import { FacebookLogin } from '../pages/facebook/facebook.component';

public userName;

constructor(fb: FacebookLogin){

    this.userName = "";
    //subscribe to the page's EventEmitter
    this.fb.userChanged.subscribe(username => {
       this.userName = username;
    });
}
Run Code Online (Sandbox Code Playgroud)

或使用EventEmitter作为Output在本作描述,以便回答:什么是正确使用的EventEmitter的?