1. fist of all, we have a interface stands for the field type

1 export interface User{
2   username: string;
3   age: number;
4 }

 

2. then, we generate a service that can be used as bridge between these components

@Injectable()
export class CurrentUserStoreService {
  // initial value with nnull or something else, for example undefined
  private currentUserSubject = new BehaviorSubject<User>(null);

  // some one might subscribe this to get real-time value, as below 
  public currentUserSubject$ = this.currentUserSubject.asObservable();
  
  // some one might use this to change value
  setCurrentUser(user: User) {
    this.currentUserSubject.next(user);
  }
}

 

3. we need to bind a user to our html, for example a header that has status of current user

 1 @Component({
 2   selector: 'app-root',
 3   templateUrl: './app.component.html',
 4   styleUrls: ['./app.component.css'],
 5   // provide our service here 
 6   providers: [CurrentUserStoreService]
 7 })
 8 export class AppComponent {
 9   user: User;
10 
11   constructor( private currentUserStoreService: CurrentUserStoreService) {
12     //subcribe and bind the value to our field 
13     currentUserStoreService.currentUserSubject$.subscribe(value => {
14       this.user = value;
15     });
16   }
17 
18  //bypass this unless you have read and understand the  contents in file userLoginComponent.ts,  if user has logged out , just update user ,  pass a null value to user
19   logout():void {
20   // now some one has subscribed it will get null value
21   this.currentUserStoreService.setCurrentUser(null);
22 }
23 
24 
25 }

 

4. now a user have login in , and we need to save the user, then update the status in header( the same as im component AppComponent ), and redirect the page to path ''

 1 @Component({
 2   selector: 'app-login',
 3   templateUrl: './user-login.component.html',
 4   styleUrls: ['./user-login.component.css']
 5   // wo don't provide service here
 6 })
 7 export class UserLoginComponent {
 8    constructor( 
 9     private router: Router,
10     private currentUserStoreService: CurrentUserStoreService ) { }
11    login():void {
12     user: User = ......//login process
13     // we there pass the user to service and it hleps broadcast this value to someone has subscribed it 
14     this.currentUserStoreService.setCurrentUser(user);
15     // we don't user location.href to redirect ( which means reload the entire application ), just use router
16     this.router.navigateByUrl('');
17    }
18 
19 }

tips: 

  Set the currentUserStoreSerivce to  @Injectable({ provide: 'root' })  to make it visible in the whole application , but in most case don't do this. In this case   UerLoginComponent  was wrapped in  AppComponent  by using tag  <router-outlet></router-outlet>  in  app.component.html  , when it needs a service it looks up to its upper components (not parent) , as we can see using chrome extension  Angury

 

Check this at gist   

posted on 2019-06-17 14:07  四维胖次  阅读(82)  评论(0编辑  收藏  举报