简体   繁体   English

根据用户类型为 angular 应用程序动态 Header

[英]Dynamic Header for angular app based on user type

So I am creating a SPA on angular and i have to change the header based on the user.所以我在 angular 上创建了一个 SPA,我必须根据用户更改 header。 I created an archiecture component which has the header and footer and i have set it to the app component with the selector so that it is the starting point.我创建了一个架构组件,它有 header 和页脚,我用选择器将它设置为应用程序组件,这样它就是起点。 So on start architecture component (header footer only) will check with ngOnInIt if user has logged in and if not navigate to login if user is logged in navigate to Home.因此,在启动架构组件(仅限页眉页脚)时,如果用户已登录,将使用 ngOnInIt 检查,如果用户已登录,则导航至登录导航至主页。 I have set ngIf on header <li> to show if student, lecturer or staff is logged in. But the problem is i cant reload on other pages or it will route to either login page or home page after checking if user is logged in cause the architecture (header footer ) component is used as the starting point and called from app component with selector.我已经在 header <li> 上设置了 ngIf 以显示学生、讲师或教职员工是否已登录。但问题是我无法在其他页面上重新加载,或者在检查用户是否已登录后它将路由到登录页面或主页,因为架构(页眉页脚)组件用作起点,并使用选择器从应用程序组件调用。 Furthermore i have to call此外,我必须打电话

reloadPage(): void {
  window.location.reload();
}

to load the header based on the user after logging in and before navigating to home page.在登录后和导航到主页之前,根据用户加载 header。

I have found some content on ng-template and using observables to deal with this but im kind of confused on it.我在 ng-template 上找到了一些内容并使用 observables 来处理这个但我有点困惑。 I hope someone can help me on this problem.我希望有人可以帮助我解决这个问题。 As im new to angular and server side programming what i say and have used might be incorrect and confusing so if theres any advices on routing, services, subscribing etc ill be really thankful!!作为 angular 和服务器端编程的新手,我所说和使用的内容可能不正确且令人困惑,所以如果有任何关于路由、服务、订阅等的建议,我将不胜感激!

EDIT............................................................................编辑................................................. .........................

Alright So the project i am creating is an online learning platform for a institution so in app component i have given the router outlet.好吧,所以我正在创建的项目是一个机构的在线学习平台,所以在应用程序组件中我给了路由器插座。 What i want is on loading the site to route to login if user is not logged in and is not contained within the storageService i created and used to check if userLoggedIn on ngOnInIt so then after validating login i want to navigate to architecture and then to home page with the architecture component displaying only the required header items for the logged in user type.我想要的是如果用户未登录并且不包含在我创建的存储服务中并用于检查 userLoggedIn 是否在 ngOnInIt 上,那么加载站点以路由到登录,然后在验证登录后我想导航到体系结构然后到家带有架构组件的页面仅显示登录用户类型所需的 header 项目。 Im struggling on coming up with the right logic to achieve this)))我正在努力想出正确的逻辑来实现这一目标)))

A very simple example:一个非常简单的例子:

Build a UserService :构建UserService

export interface User {
  name: string;
  password: string;
  role: string;
}

export class UserService {
  private currentUserSubject: Subject<User> = new Subject<User>();

  getCurrentUserSub() {
    return this.currentUserSubject.asObservable();
  }

  login() { // Here we fake a login for this example
    const loggedInUser = { name: "Testuser", passwort: "Secret", role: "SuperAdmin" };

    this.currentUserSubject.next(loggedInUser); // Set the user
  }
}

Now the HeaderComponent (or any other)现在HeaderComponent (或任何其他)

...
export class HeaderComponent implements onInit, onDestroy {
  currentUser!: User;
  userSub!: Subscription;

  constructor(private userService: UserService) {}

  ngOnInit(): void {
    this.userSub = this.userService.getCurrentUserSub().subscribe(data => {
      this.currentUser = data; // Every time the user changed in the Service, we get info
    });
  }

  ngOnDestory(): void {
    this.userSub.unsubscribe(); // Important!
  }
}

Headers HTML标头 HTML

<div *ngIf="currentUser">
  Hello {{ currentUser.name }}! Your role is <b>{{ currentUser.role }}</b>
</div>
<div *ngIf="!currentUser">
  Nobody is logged in.
</div>

Now in all other Components you can (if needed) bind the user in the same way like in HeaderComponent .现在,在所有其他Components中,您可以(如果需要)以与HeaderComponent中相同的方式绑定用户。

In LoginComponent we now call the login method:LoginComponent ,我们现在调用登录方法:

...
  login() {
    this.userService.login();
  }
...

So all components they subsribed to the LoginService currentUserSubject (getCurrentUserSub() - use always a Observable never directly a Subject ) get the newest data automatically.因此,他们订阅LoginService currentUserSubject的所有组件(getCurrentUserSub() - 始终使用Observable不是直接使用Subject )自动获取最新数据。

In ngOnDestroy we unsubscribe .ngOnDestroy我们取消订阅 If the component will destroyed we don't wanna updates any more (Memory Leaks or multiple subscritpion calls).如果组件将被销毁,我们不想再更新(内存泄漏或多次订阅调用)。

A very much bigger Project with this schema is here on Stackblitz . Stackblitz上有一个具有此模式的更大项目。 But the principle is the same - is always the same way.但原则是一样的——总是一样的方式。

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

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