为什么代码段会造成内存泄漏?
Why the code segment is creating memory leak?
我正在使用 http 服务从 UserService 获取用户数据。当我订阅可观察对象时,它会造成内存泄漏。在开发人员控制台的网络选项卡中,可以看到正在发出无限的 http 请求。
尝试在 ngOnDestroy() 方法中取消订阅,但没有成功。
user.service.ts
getCurrentUserDetails(){
return this.http.get<User>(`${this.config.apiUrl}/user/me`);
}
navbar.component.ts
export class NavbarComponent implements OnInit, OnDestroy {
user: User;
userDetailsSubs: Subscription;
constructor(
private router: Router,
private authenticationService: AuthenticationService,
private userService: UserService
) { }
ngOnInit() {
}
isLoggedIn() {
const currentUser: User = this.authenticationService.currentUserValue;
if (currentUser) {
this.userDetailsSubs = this.userService.getCurrentUserDetails()
.subscribe(
data => {
this.user = data;
}
, error => {
console.log(error);
});
//this.userDetailsSubs.unsubscribe();
return true;
}
else
return false;
}
logout() {
this.authenticationService.logout();
this.router.navigate(['/login']);
}
ngOnDestroy() {
this.userDetailsSubs.unsubscribe();
}
}
navbar.component.html
<div class="sticky-top mb-3">
<nav class="navbar navbar-expand-lg navbar-dark bg-danger justify-content-center p-3 mb-3" *ngIf="!isLoggedIn()">
..............
</nav>
<nav class="navbar navbar-expand navbar-dark bg-danger p-3 mb-5" *ngIf="isLoggedIn()">
.
.
.
</nav>
</div>
您的 isLoggedIn()
函数是在更改检测时从模板触发的。将 isLogged
属性 添加到您的组件并从 ngOnInit 中设置它以避免多次调用。
isLogged: boolean;
ngOnInit() {
const currentUser: User = this.authenticationService.currentUserValue;
if (currentUser) {
this.userDetailsSubs = this.userService.getCurrentUserDetails()
.subscribe(
data => {
this.isLogged = true;
this.user = data;
}
, error => {
console.log(error);
});
}
else
this.isLogged = false;
}
在你的 class 中创建一个 属性,比如 isLoggedInFlag
。'
根据您的 isLoggedIn()
方法,在订阅下将其设置为 true。
你的观点是:
<nav class="navbar navbar-expand-lg navbar-dark bg-danger justify-content-center p-3 mb-3" *ngIf="!isLoggedInFlag">
<nav class="navbar navbar-expand navbar-dark bg-danger p-3 mb-5" *ngIf="isLoggedInFlag">
不要在 HTML 上调用这样的方法。
*ngIf="isLoggedIn()"
会一直执行这个方法
我正在使用 http 服务从 UserService 获取用户数据。当我订阅可观察对象时,它会造成内存泄漏。在开发人员控制台的网络选项卡中,可以看到正在发出无限的 http 请求。
尝试在 ngOnDestroy() 方法中取消订阅,但没有成功。
user.service.ts
getCurrentUserDetails(){
return this.http.get<User>(`${this.config.apiUrl}/user/me`);
}
navbar.component.ts
export class NavbarComponent implements OnInit, OnDestroy {
user: User;
userDetailsSubs: Subscription;
constructor(
private router: Router,
private authenticationService: AuthenticationService,
private userService: UserService
) { }
ngOnInit() {
}
isLoggedIn() {
const currentUser: User = this.authenticationService.currentUserValue;
if (currentUser) {
this.userDetailsSubs = this.userService.getCurrentUserDetails()
.subscribe(
data => {
this.user = data;
}
, error => {
console.log(error);
});
//this.userDetailsSubs.unsubscribe();
return true;
}
else
return false;
}
logout() {
this.authenticationService.logout();
this.router.navigate(['/login']);
}
ngOnDestroy() {
this.userDetailsSubs.unsubscribe();
}
}
navbar.component.html
<div class="sticky-top mb-3">
<nav class="navbar navbar-expand-lg navbar-dark bg-danger justify-content-center p-3 mb-3" *ngIf="!isLoggedIn()">
..............
</nav>
<nav class="navbar navbar-expand navbar-dark bg-danger p-3 mb-5" *ngIf="isLoggedIn()">
.
.
.
</nav>
</div>
您的 isLoggedIn()
函数是在更改检测时从模板触发的。将 isLogged
属性 添加到您的组件并从 ngOnInit 中设置它以避免多次调用。
isLogged: boolean;
ngOnInit() {
const currentUser: User = this.authenticationService.currentUserValue;
if (currentUser) {
this.userDetailsSubs = this.userService.getCurrentUserDetails()
.subscribe(
data => {
this.isLogged = true;
this.user = data;
}
, error => {
console.log(error);
});
}
else
this.isLogged = false;
}
在你的 class 中创建一个 属性,比如 isLoggedInFlag
。'
根据您的 isLoggedIn()
方法,在订阅下将其设置为 true。
你的观点是:
<nav class="navbar navbar-expand-lg navbar-dark bg-danger justify-content-center p-3 mb-3" *ngIf="!isLoggedInFlag">
<nav class="navbar navbar-expand navbar-dark bg-danger p-3 mb-5" *ngIf="isLoggedInFlag">
不要在 HTML 上调用这样的方法。
*ngIf="isLoggedIn()"
会一直执行这个方法