簡體   English   中英

如何將JSON api接收到的數據分配給打字稿中的變量?

[英]How to assign JSON api received data to a variable in typescript?

我的標題說明了一切,我調用了一個API,該API反過來以JSON格式發送數據,我希望將其分配給變量。 這是我的ajax代碼。 記住:我在Angular應用程序中使用它

我嘗試將其分配給變量,但沒有成功。

$.ajax({
  url: 'https://randomuser.me/api/',
  dataType: 'json',
  success: function(data) {
    console.log(data);
  }
});

您將必須使用箭頭功能才能獲得正確的上下文,例如:

$.ajax({
  url: 'https://randomuser.me/api/',
  dataType: 'json',
  success: (data) => {
    console.log(data);
    this.abcVariable = data; //<====== Here

  }
});

注意:在Angular應用程序中使用jQuery並不是一個好習慣,Angular提供了用於網絡事物的http模塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM