如何在angularv8中显示来自服务器的数据

How to display data from server in angularv8

我想显示登录应用程序的人的姓名。我试过了

getprotected() { 
return this.http.get(${this.url}/api/protected_things)
   .pipe( 
      catchError(e => { 
         let status = e.status; 
         if (status === 401) { 
            this.showAlert('You are not authorized for this!'); 
            this.logout(); 
         } 
         throw new Error(e);
      }) 
   ) 
} 

tab1.ts

export class Tab1Page implements OnInit {

  data = '';

  constructor(private authService: AuthService, private storage: Storage, private toastController: ToastController, private http: HttpClient) { }

  ngOnInit() {

      this.authService.getprotected().subscribe(res => {
       this.data = res['name'];
       });
  }

tab1.html

<p class="ion-text-center">Bienvenu</p>

  <p class="ion-text-center"><b>{{ data }}</b></p>

  <ion-button expand="full" (click)="logout()">Deconnecte Toi</ion-button>

但无法正常工作。 谢谢

您应该尝试使用 res.name 访问您需要的数据。

根据一般经验,请尝试记录您的结果。 .subscribe() 中的简单 console.log(res) 将帮助您形象化收到的对象。

如果问题发生得早,chrome 网络检查员将帮助您检测您收到的数据。请记住,空对象不会触发您的捕获错误。要了解有关 chrome 中网络选项卡的更多信息:https://www.youtube.com/watch?v=e1gAyQuIFQo&

顺便说一句,你应该看一些教程,在 youutbe 上你可以找到很棒的 material