简体   繁体   English

如何将数据从登录服务传递到Angular中的NavBar组件?

[英]How to pass data from login service to NavBar component in Angular?

After user logs in, I have to update the username in the navbar component.My component structure is as follow. 用户登录后,我必须更新导航栏组件中的用户名。我的组件结构如下。 app.component.ts app.component.ts

<app-root>
  <nav-bar></nav-bar>
  <router-outlet></router-outlet>  // <-- Login form comes here
</app-root>

nav-bar.html NAV-一个bar.html

 <ul class="nav navbar-nav">
    <li ><h4 style="display:inline-block">Welcome ,</h4><b>{{username}}</b></li>
    <li *ngIf="!isLoggedIn"><a (click)="logout()">Log out</a></li>
 </ul>

login-service.ts 登录-service.ts

@Injectable()
export class LoginService  {
 login(un,pwd) {
   ...
 }
}

How to share data between these sibling components? 如何在这些兄弟组件之间共享数据?

I think you should use behavior subject for this in your service file write a code like this 我认为您应该在服务文件中为此使用行为主题,编写这样的代码

export class loginService {
        name:Subject<string> = new Subject();

        broadcastLoginChange(text:string) {
            this.name.next(text);
        }

In your login component you should use this code 在登录组件中,您应该使用此代码

this.loginService.broadcastLoginChange(username)

and in your navbar subscribe to this service like this 并在您的导航栏中订阅这样的服务

this.logiService.name.subscribe((val) => {
      this.username=val;
    });

this will help you as it will work on change as your login name will change the username in the navbar will automatically change 这将对您有所帮助,因为它可以更改,因为您的登录名将更改,导航栏中的用户名将自动更改

login-service.ts 登录-service.ts

@Injectable()
export class LoginService  {
 login(un,pwd) {
   // your implementation here
  return un;
 }
}

nav-bar.ts NAV-bar.ts

   constructor(public loginService:LoginService  ){}
     this.loginService.login().subscribe(
          res=> {
            this.loaddata(res)
            }, err => {
           console.log(err);
            });
    loaddata(un){
    console.log(un);
  this.username = un;
    }

nav-bar.html NAV-一个bar.html

 <ul class="nav navbar-nav">
    <li ><h4 style="display:inline-block">Welcome ,</h4><b>{{username}}</b></li>
    <li *ngIf="!isLoggedIn"><a (click)="logout()">Log out</a></li>
 </ul>

Modify the service and store current user: 修改服务并存储当前用户:

@Injectable()
    export class LoginService {
      currentUser: any;

      login(un,pwd) {
       this.currentUser = un;
      }
    }

Include the LoginService in the provider app.module.ts 在提供程序app.module.ts中包含LoginService

import { LoginService } from "app/services/login.service";
    @NgModule({
      declarations: []
    providers: [
        LoginService
    ]
    })
    export class AppModule { }

Now you can use currentUser variable of LoginService inside nav-bar component 现在,您可以在导航栏组件中使用LoginService的currentUser变量

export class NavBarComponent {
 constructor(
    private authService:AuthenticationService
    ) {}
}

The advantage of this approach: 这种方法的优点:

  • Can use login detail at multiple pages. 可以在多个页面上使用登录详细信息。
  • Dynamic update when login detail change 登录详细信息更改时动态更新
  • Separation of login logic shared across the app 跨应用共享的登录逻辑分离
  • broadcasting is not the preferable approach as it broadcast the data, any module can subscribe it and use it. 广播不是首选方法,因为它广播数据,任何模块都可以订阅并使用它。

I hope this will help you. 我希望这能帮到您。 Happy coding 快乐编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM