Auth0 昵称 属性 在 angular 中重新加载后有效 2

Auth0 nickname property works after reload in angular 2

我想在用户使用 auth0 登录时显示他的名字。当用户登录时,名称不会显示在导航栏中。但是当我们重新加载页面时,名称出现了。

app.component.ts 文件

import { Component, AfterViewInit } from '@angular/core';
import {SignupService} from './services/signup.service';
import {Auth} from './services/auth.service';
declare var $: any;

@Component({
  moduleId:module.id,
  selector: 'my-app',
  templateUrl: 'app.component.html',
  styleUrls: ['./assets/css/navbar.css'],
  providers: [SignupService],
  })
 export class AppComponent  {
  profile : any;
  constructor(private auth: Auth){

  this.profile=JSON.parse(localStorage.getItem('profile'));

};

ngAfterViewInit() {
  $(".button-collapse").sideNav();
  $(".dropdown-button").dropdown();


  }

}

那是因为您只在启动时收集值。

您应该:

  • re-get每隔几秒的值:
    setTimeout(() => localStorage.getItem('profile'), 1000)
  • 向您想要的使用共享变量的控制器注入一个 Auth service。因此您可以更新模板中显示的登录值。
  • 使用 EventEmitter 来调用修改适当登录控制器的函数。