我正在MYPROFILE()方法(此生成代码的方法)中使用hasProfile变量来检查用户是否具有个人资料(当他看到自己的个人资料时),并且hasProfile变量将显示一个用于创建的按钮当用户没有一个新的配置文件时。问题在于它正在获取和未定义的值,因为该变量位于可观察变量之内,甚至不评估If或console.log('hasProfile1 ??? ::',this.hasProfile);。当我放一个。
所以问题是,如何在检查可观察对象时避免未定义?有其他实现目标的方法的想法吗?
如果我添加this.hasProfile = true;在方法开始时,即使res.body为空,变量也不会改变。
import { Component, OnInit, OnDestroy } from '@angular/core';
import { HttpErrorResponse, HttpHeaders, HttpResponse } from '@angular/common/http';
import { ActivatedRoute, Router } from '@angular/router';
import { Subscription } from 'rxjs';
import { JhiEventManager, JhiParseLinks, JhiAlertService, JhiDataUtils } from 'ng-jhipster';
import { IProfile } from 'app/shared/model/profile.model';
import { Principal } from 'app/core';
import { ITEMS_PER_PAGE } from 'app/shared';
import { ProfileService } from './profile.service';
@Component({
selector: 'jhi-profile',
templateUrl: './profile.component.html'
})
export class ProfileComponent implements OnInit, OnDestroy {
currentAccount: any;
profiles: IProfile[];
error: any;
success: any;
eventSubscriber: Subscription;
routeData: any;
links: any;
totalItems: any;
queryCount: any;
itemsPerPage: any;
page: any;
predicate: any;
previousPage: any;
reverse: any;
owner: any;
isAdmin: boolean;
hasProfile: boolean;
constructor(
private profileService: ProfileService,
private parseLinks: JhiParseLinks,
private jhiAlertService: JhiAlertService,
private principal: Principal,
private activatedRoute: ActivatedRoute,
private dataUtils: JhiDataUtils,
private router: Router,
private eventManager: JhiEventManager
) {
this.itemsPerPage = ITEMS_PER_PAGE;
this.routeData = this.activatedRoute.data.subscribe(data => {
this.page = data.pagingParams.page;
this.previousPage = data.pagingParams.page;
this.reverse = data.pagingParams.ascending;
this.predicate = data.pagingParams.predicate;
});
}
loadAll() {
this.profileService
.query({
page: this.page - 1,
size: this.itemsPerPage,
sort: this.sort()
})
.subscribe(
(res: HttpResponse<IProfile[]>) => this.paginateProfiles(res.body, res.headers),
(res: HttpErrorResponse) => this.onError(res.message)
);
}
loadPage(page: number) {
if (page !== this.previousPage) {
this.previousPage = page;
this.transition();
}
}
transition() {
this.router.navigate(['/profile'], {
queryParams: {
page: this.page,
size: this.itemsPerPage,
sort: this.predicate + ',' + (this.reverse ? 'asc' : 'desc')
}
});
this.loadAll();
}
clear() {
this.page = 0;
this.router.navigate([
'/profile',
{
page: this.page,
sort: this.predicate + ',' + (this.reverse ? 'asc' : 'desc')
}
]);
this.loadAll();
}
ngOnInit() {
this.loadAll();
this.principal.identity().then(account => {
this.currentAccount = account;
this.owner = account.id;
this.principal.hasAnyAuthority(['ROLE_ADMIN']).then( result => {
this.isAdmin = result;
});
});
this.registerChangeInProfiles();
}
ngOnDestroy() {
this.eventManager.destroy(this.eventSubscriber);
}
trackId(index: number, item: IProfile) {
return item.id;
}
byteSize(field) {
return this.dataUtils.byteSize(field);
}
openFile(contentType, field) {
return this.dataUtils.openFile(contentType, field);
}
registerChangeInProfiles() {
this.eventSubscriber = this.eventManager.subscribe('profileListModification', response => this.loadAll());
}
sort() {
const result = [this.predicate + ',' + (this.reverse ? 'asc' : 'desc')];
if (this.predicate !== 'id') {
result.push('id');
}
return result;
}
private myProfile() {
const query = {
page: this.page - 1,
size: this.itemsPerPage,
sort: this.sort()
};
if ( this.currentAccount.id != null) {
query['userId.equals'] = this.currentAccount.id;
}
console.log('Estoy entrando en el observalbe:');
this.profileService
.query(query)
.subscribe(
(res: HttpResponse<IProfile[]>) => {
if ( !res.body ) {
this.hasProfile = false;
}
this.paginateProfiles(res.body, res.headers);
} , (res: HttpErrorResponse) => this.onError(res.message)
);
console.log('hasProfile???:', this.hasProfile);
}
private paginateProfiles(data: IProfile[], headers: HttpHeaders) {
this.links = this.parseLinks.parse(headers.get('link'));
this.totalItems = parseInt(headers.get('X-Total-Count'), 10);
this.queryCount = this.totalItems;
this.profiles = data;
console.log('OWNER', this.owner);
console.log('isADMIN', this.isAdmin);
console.log('PROFILES:', this.profiles);
}
private onError(errorMessage: string) {
this.jhiAlertService.error(errorMessage, null, null);
}
}
谢谢