【问题标题】:how will one angular component tell another to refresh without manually refreshing whole page一个角度组件如何在不手动刷新整个页面的情况下告诉另一个组件刷新
【发布时间】:2019-10-18 14:21:49
【问题描述】:

我有一个带有 Firebase 身份验证的节点后端,它连接到一个角度前端。我有两个组件,即导航栏和注册表单,当我注册时,我调用后端函数在 firebase 中注册并发回 firebase.auth().currentUser,我的角度前端导航栏应该如何检测到此类用户的到来和相应地更新自身,无需我刷新页面?

【问题讨论】:

  • 您可以使用共享状态或subscription 来实现此目的
  • 不确定如何使用 Agnular 完成此操作,但在 AngularJS 中有一个 hack,您只需编译并链接组件,然后使用 JQLite 将旧组件替换为新组件。 (angular.element('component').replaceWith($compile('<component></component>')($rootScope.$new())))。但这只是您所要求的破解,这不是解决方案,您应该能够使用适当的路由器做您需要的事情。

标签: node.js angular firebase firebase-authentication


【解决方案1】:

我认为这就是您的组件的结构方式:

// In app.component.html
<app-navbar></app-navbar>
<router-outlet></router-outlet>   // For rest of the pages.

在这里,我们可以通过两种方式(我不会谈论包括 ngModel)将数据传递给导航栏以更新导航栏的状态/数据。

1.使用输入/输出装饰器。

// Your navbar.component.ts

// I'm assuming you have set up model for User. If no, then change the type to **any**.
@Input() currentUser: IUser;   

// Your navbar.component.html
<p *ngIf="currentUser"> {{currentUser | json}} </p>   // Render this if currentUser exists. By default, it is undefined.

现在,在您的应用组件中:

//In your app.component.ts

User: any | IUser;

public authenticateUser() {
 this.someServiceToAuthenticateUser.subscribe(
  res => this.User = res,    
  error => console.error(error)
);
}


 // In your app.component.html
<app-navbar [currentUser]="User"></app-navbar>
<router-outlet></router-outlet>
  1. 使用公共服务。
    您可以创建将经过身份验证的用户存储在其中的公共服务。在您的App 组件中,您可以将用户设置为成功验证响应,并且从其他组件中,您可以获取经过验证的用户。
export class UserService {
 private currentUser: IUser | any;

 setCurrentUser(userToSet: any) {
    this.currentUser = userToSet;
}

getCurrentUser() {
 return this.currentUser;
}

现在,在你的App组件中,如果认证使用请求成功,则设置UserService的currentUser:

//In your app.component.ts

User: any | IUser;

public authenticateUser() {
 this.someServiceToAuthenticateUser.subscribe(
  res => this.UserService.setCurrentUser(res),    
  error => console.error(error)
);
}

现在,在您的导航栏组件中,从 UserService 获取当前用户。

// In navbar.component.ts

 currentUser: any;

constructor(private userServicce: UserService) {
 getuserFromservice();
}

 public getuserFromservice() {
 setInterval(() => {
  curretnUser = this.userService.getCurrentUser()
}, 3000);

重要提示:在这种方法中,请注意在导航栏组件中,我在 setInterval 中设置了当前用户。这是因为您的 getuserFromservice() 只会运行一次(当它第一次创建/渲染时),此时 userService 将没有 currentUser。所以导航栏永远不会得到当前用户。 如果您想替换 setInterval 方法(我强烈建议替换它),请使用 Subjects/BehaviourSubject/ReplaySubject。我没有在这里包含它们,因为 Rxjs 本身就是一个巨大的话题,在这里包含它会太难理解。

【讨论】:

    【解决方案2】:

    Input & Output 算子不错,但不能像 observable 那样灵活地共享数据

    这里您需要使用 Subject Observable 在应用程序之间共享数据。这是非常简单和有用的事情。当用户注册时,您可以向所有 lisner 发送信号。

    你需要做什么

    1) 创建一个服务文件来共享数据 - 服务有可观察的主题。使用它,您可以订阅用户数据的频道

    2) 在您的标头组件中侦听该服务并将侦听器传入的数据设置为标头用户数据

    3) 从您的注册、个人资料更新或其他任何用户数据已更新的地方,只要将这些数据传递到可观察对象,它将自动更新整个应用程序中的用户。按照下面的例子来实现这个

    您可以在下面的链接中找到工作演示

    https://jasonwatmore.com/post/2019/02/07/angular-7-communicating-between-components-with-observable-subject

    【讨论】:

      猜你喜欢
      • 2020-11-14
      • 1970-01-01
      • 2023-04-01
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多