我正在尝试在 template 中显示 Object 。在控制台中用户.email正在工作,但在模板中它不起作用。是否必须定义 Users 对象的类型。如何正确处理响应。在控制台中,数据显示为用户对象,我从中获取第一个对象作为数据[0]。
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AuthenticationService } from './services/authentication.service';
import { EmployeeService } from './services/emplyee.service';
@Component({
template: `<h2>Welcome, {{ Users.email }}</h2>
<button class="btn btn-info bg-blue bg-darken-2 mt-1 min-width-150" (click)="logout()"><i class="icon-unlock2"></i>
Logout
</button>`,
})
export class homeComponent implements OnInit {
Users: any;
constructor(private authenticationService: AuthenticationService,
private router: Router, private
emplyeeService: EmployeeService) {
}
ngOnInit() {
if (!localStorage.getItem('currentUser')) {
this.router.navigate(['/']);
console.log("Go Login");
} else {
this.getUsers();
}
}
logout() {
this.authenticationService.logout();
this.router.navigate(['/']);
}
getUsers() {
this.emplyeeService.getAll()
.subscribe(
(data: any) => {
console.log(data);
this.Users = data[0];
console.log(this.Users.email);
},
(error) => console.log(error)
);
}
}
您使用 Observables,因此数据在时间上会有一点延迟,因此您需要输入安全的操作符?
如下
<h2>Welcome, {{ Users?.email }}</h2>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)