Pin*_*ski 14 typescript angular2-changedetection angular
所以我有一个标题组件,显示用户名或"登录",具体取决于他们是否登录.我还有一个Login组件,它执行登录的所有业务逻辑.它们目前没有父/子关系.
当用户登录时,除非在浏览器中完成整页刷新,否则标题不会刷新或更改.我一直在网上搜索和阅读有关不同方法的大量搜索.ngOnChanges,NgZone,ApplicationRef和ChangeDetectorRef似乎是最受欢迎的.我正在尝试在ChangeDetectorRef中实现此行为,因为这似乎与我的情况最相关.但是,我似乎无法找到如何使用它的实际示例.
我把它编码了,但它似乎没有做任何事情.任何意见,将不胜感激.我甚至接受我采取错误的方法,除了ChangeDetectorRef之外还需要使用另一种解决方案.
LoginComponent
import { Component, OnInit } from '@angular/core';
import { Response } from '@angular/http';
import { Router } from '@angular/router';
import { AuthenticationService } from '../service/authentication.service';
@Component({
selector: 'login-component',
templateUrl: './login.component.html'
})
export class LoginComponent implements OnInit {
constructor(private router: Router,
private authenticationService: AuthenticationService) { }
ngOnInit() {
// Resets the login service.
// This is one of the events that should cause the refresh.
this.authenticationService.logout();
}
login() {
/*
Authentication code
This is the other event that should cause the refresh.
*/
}
}
Run Code Online (Sandbox Code Playgroud)
HeaderComponent
import { ChangeDetectorRef, ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { Instance } from '../../entity/instance';
@Component({
selector: 'header-component',
templateUrl: './html/header.component.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class HeaderComponent {
userName: string;
constructor(private ref: ChangeDetectorRef) {
this.ref.markForCheck();
}
ngOnInit(): void {
var currentUser = JSON.parse(localStorage.getItem('currentUser'));
this.userName = currentUser && currentUser.full_name;
if (!this.userName) {
this.userName = "User Name";
}
}
}
Run Code Online (Sandbox Code Playgroud)
AppComponent
import { ChangeDetectorRef, ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { Instance } from './entity/instance';
import { InstanceService } from './service/instance.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class AppComponent implements OnInit {
instances: Instance[];
constructor(private instanceService: InstanceService) { }
ngOnInit(): void {
}
}
Run Code Online (Sandbox Code Playgroud)
app.component.html
<header-component></header-component>
<router-outlet></router-outlet>
Run Code Online (Sandbox Code Playgroud)
Pin*_*ski 19
所以我最终采取了一些使用我的服务来发出变化的建议.我在Stack Overflow上的某些地方读到,使用这种方式的服务是一种糟糕的模式,只能从子组件发送到父组件.所以我不确定这是"正确的"方式,但它对我有用,因为我想知道这个事件的多个组件.
我已经有一个处理我的身份验证的服务,所以我所要做的就是给它一个发射器,在适当的时间发出,然后在我的组件中监听emit.
标题组件
export class HeaderComponent {
userName: string;
constructor(private authenticationService: AuthenticationService) {
authenticationService.getLoggedInName.subscribe(name => this.changeName(name));
}
private changeName(name: string): void {
this.userName = name;
}
}
Run Code Online (Sandbox Code Playgroud)
验证服务
@Injectable()
export class AuthenticationService {
@Output() getLoggedInName: EventEmitter<any> = new EventEmitter();
login(email: string, password: string): Observable<boolean> {
if (successfulLogIn(email, password)) {
this.getLoggedInName.emit(fullName);
return true;
} else {
this.getLoggedInName.emit('Sign In');
return false;
}
}
logout(): void {
this.getLoggedInName.emit('Sign In');
}
}
Run Code Online (Sandbox Code Playgroud)
@Pinski很好。但这可以更容易。这是发出和预订数据的替代方法。
标头组件
export class HeaderComponent implements OnInit {
userName: string;
constructor(private authenticationService: AuthenticationService) {}
NgOnInit() {
this.authenticationService.getLoggedInName.subscribe(name => this.userName = name);
}
}
Run Code Online (Sandbox Code Playgroud)
认证服务
@Injectable()
export class AuthenticationService {
public getLoggedInName = new Subject(); //Alternate method to Emitting data across Components. Subject() is doing both Emitting data and Subscribing it in another component. So its the best way to compare with Emitting using Output.
login(email: string, password: string): Observable<boolean> {
if (successfulLogIn(email, password)) {
this.getLoggedInName.next(fullName); //next() method is alternate to emit().
return true;
} else {
this.getLoggedInName.next('Sign In');
return false;
}
}
logout(): void {
this.getLoggedInName.next('Sign In');
}
}
Run Code Online (Sandbox Code Playgroud)
尝试Subject()。快乐的编码。
| 归档时间: |
|
| 查看次数: |
20294 次 |
| 最近记录: |