You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/v2/cookbook/using-axios-to-consume-apis.md
+25-25Lines changed: 25 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,16 @@
1
1
---
2
-
title: Using Axios to Consume APIs
2
+
title: Menggunakan Axios untuk Mengkonsumsi API
3
3
type: cookbook
4
4
order: 9
5
5
---
6
6
7
-
## Base Example
7
+
## Contoh Dasar
8
8
9
-
There are many times when building application for the web that you may want to consume and display data from an API. There are several ways to do so, but a very popular approach is to use[axios](https://github.com/axios/axios), a promise-based HTTP client.
9
+
Seringkali ketika membangun aplikasi untuk web, Anda mungkin ingin menggunakan dan menampilkan data dari sebuah API. Ada beberapa cara untuk melakukannya, namun pendekatan yang sangat populer adalah dengan menggunakan[axios](https://github.com/axios/axios), sebuah _HTTP client_ berbasis promise.
10
10
11
-
In this exercise, we'll use the[CoinDesk API](https://www.coindesk.com/api/)to walk through displaying Bitcoin prices, updated every minute. First, we'd install axios with either npm/yarn or through a CDN link.
11
+
Dalam latihan ini, kita akan menggunakan[CoinDesk API](https://www.coindesk.com/api/)untuk menampilkan harga Bitcoin, yang diperbarui setiap menit. Pertama, kita akan pasang axios dengan npm/yarn atau melalui tautan CDN.
12
12
13
-
There are a number of ways we can request information from the API, but it's nice to first find out what the shape of the data looks like, in order to know what to display. In order to do so, we'll make a call to the API endpoint and output it so we can see it. We can see in the CoinDesk API documentation, that this call will be made to `https://api.coindesk.com/v1/bpi/currentprice.json`. So first, we'll create a data property that will eventually house our information, and we'll retrieve the data and assign it using the `mounted` lifecycle hook:
13
+
Kita dapat melakukan permintaan informasi dari API dengan beberapa cara, namun sebaiknya kita pahami dulu format data nya, agar kita mengetahui apa yang akan ditampilkan. Untuk melakukan nya, kita akan melakukan panggilan ke API dan menampilkannya sehingga kita dapat melihatnya. Kita dapat melihat pada dokumentasi CoinDesk API, panggilan ini akan dilakukan ke `https://api.coindesk.com/v1/bpi/currentprice.json`. Jadi, pertama, kita akan membuat sebuah properti data yang pada akhirnya akan menyimpan informasi kita, dan kita akan mengambil data dan menetapkannya menggunakan kait siklus hidup `mounted`:
14
14
15
15
```js
16
16
newVue({
@@ -34,18 +34,18 @@ new Vue({
34
34
</div>
35
35
```
36
36
37
-
And what we get is this:
37
+
Dan kita mendapatkan seperti berikut:
38
38
39
39
<pdata-height="350"data-theme-id="32763"data-slug-hash="80043dfdb7b90f138f5585ade1a5286f"data-default-tab="result"data-user="Vue"data-embed-version="2"data-pen-title="First Step Axios and Vue"class="codepen">See the Pen <ahref="https://codepen.io/team/Vue/pen/80043dfdb7b90f138f5585ade1a5286f/">First Step Axios and Vue</a> by Vue (<ahref="https://codepen.io/Vue">@Vue</a>) on <ahref="https://codepen.io">CodePen</a>.</p>
Excellent! We've got some data. But it looks pretty messy right now so let's display it properly and add some error handling in case things aren't working as expected or it takes longer than we thought to get the information.
42
+
Luar biasa! Kita sudah mendapatkan beberapa data. Namun saat ini data tersebut terlihat berantakan, jadi mari kita tampilkan dengan benar dan kita tambahkan beberapa penanganan kesalahan/galat jika sesuatu tidak berjalan sesuai yang diharapkan atau memerlukan waktu lebih lama dari yang kita pikirkan untuk mendapatkan informasi tersebut.
43
43
44
-
## Real-World Example: Working with the Data
44
+
## Contoh Dunia Nyata: Bekerja dengan Data
45
45
46
-
### Displaying Data from an API
46
+
### Menampilkan Data dari API
47
47
48
-
It's pretty typical that the information we'll need is within the response, and we'll have to traverse what we've just stored to access it properly. In our case, we can see that the price information we need lives in`response.data.bpi`. If we use this instead, our output is as follows:
48
+
Pada umumnya informasi yang kita perlukan berada dalam respon, dan kita harus menelusuri apa yang baru saja kita simpan untuk dapat mengaksesnya dengan benar. Dalam kasus kita, kita dapat melihat bahwa informasi harga yang kita perlukan ada di`response.data.bpi`. Jika kita menggunakan ini, kita menampilkan sebagai berikut:
49
49
50
50
```js
51
51
axios
@@ -56,7 +56,7 @@ axios
56
56
<pdata-height="200"data-theme-id="32763"data-slug-hash="6100b10f1b4ac2961208643560ba7d11"data-default-tab="result"data-user="Vue"data-embed-version="2"data-pen-title="Second Step Axios and Vue"class="codepen">See the Pen <ahref="https://codepen.io/team/Vue/pen/6100b10f1b4ac2961208643560ba7d11/">Second Step Axios and Vue</a> by Vue (<ahref="https://codepen.io/Vue">@Vue</a>) on <ahref="https://codepen.io">CodePen</a>.</p>
This is a lot easier for us to display, so we can now update our HTML to display only the information we need from the data we've received, and we'll create a[filter](../api/#Vue-filter)to make sure that the decimal is in the appropriate place as well.
59
+
Ini jauh lebih mudah bagi kita untuk menampilkannya, jadi sekarang kita dapat melakukan pembaruan HTML kita agar hanya menampilkan informasi yang kita butuhkan dari data yang kita terima, dan kita akan membuat[filter](../api/#Vue-filter)untuk memastikan bahwa nilai desimal juga tepat.
60
60
61
61
```html
62
62
<divid="app">
@@ -84,15 +84,15 @@ filters: {
84
84
<pdata-height="300"data-theme-id="32763"data-slug-hash="9d59319c09eaccfaf35d9e9f11990f0f"data-default-tab="result"data-user="Vue"data-embed-version="2"data-pen-title="Third Step Axios and Vue"class="codepen">See the Pen <ahref="https://codepen.io/team/Vue/pen/9d59319c09eaccfaf35d9e9f11990f0f/">Third Step Axios and Vue</a> by Vue (<ahref="https://codepen.io/Vue">@Vue</a>) on <ahref="https://codepen.io">CodePen</a>.</p>
There are times when we might not get the data we need from the API. There are several reasons that our axios call might fail, including but not limited to:
89
+
Terkadang kita mungkin tidak mendapatkan data yang kita inginkan dari API. Ada beberapa alasan mengapa panggilan menggunakan axios gagal, termasuk namun tidak terbatas pada:
90
90
91
-
*The API is down.
92
-
*The request was made incorrectly.
93
-
*The API isn't giving us the information in the format that we anticipated.
91
+
* API tidak berfungsi.
92
+
*Permintaan dibuat dengan tidak benar.
93
+
* API tidak memberikan kita informasi dengan format yang kita antisipasi.
94
94
95
-
When making this request, we should be checking for just such circumstances, and giving ourselves information in every case so we know how to handle the problem. In an axios call, we'll do so by using`catch`.
95
+
Ketika membuat permintaan, kita harus memeriksa kondisi seperti itu, dan memberi diri kita informasi pada setiap kasus sehingga kita tahu cara menangani masalah tersebut. Dalam panggilan axios, kita akan melakukannya dengan`catch`.
96
96
97
97
```js
98
98
axios
@@ -101,7 +101,7 @@ axios
101
101
.catch(error=>console.log(error))
102
102
```
103
103
104
-
This will let us know if something failed during the API request, but what if the data is mangled or the API is down? Right now the user will just see nothing. We might want to build a loader for this case, and then tell the user if we're not able to get the data at all.
104
+
Ini akan memberi tahu kita jika sesuatu gagal selama permintaan API, namun bagaimana jika datanya rusak atau API tidak berfungsi? Sekarang pengguna tidak akan melihat apa-apa. Kita mungkin ingin membuat _loader_ untuk kasus ini, kemudian memberi tahu pengguna bahwa kita tidak dapat mendapatkan data sama sekali.
105
105
106
106
```js
107
107
newVue({
@@ -159,21 +159,21 @@ new Vue({
159
159
</div>
160
160
```
161
161
162
-
You can hit the rerun button on this pen to see the loading status briefly while we gather data from the API:
162
+
Anda dapat menekan tombol _re-run_ pada _pen_ ini untuk melihat status memuat secara singkat sambil kita mengumpulkan data dari API.
163
163
164
164
<pdata-height="300"data-theme-id="32763"data-slug-hash="6c01922c9af3883890fd7393e8147ec4"data-default-tab="result"data-user="Vue"data-embed-version="2"data-pen-title="Fourth Step Axios and Vue"class="codepen">See the Pen <ahref="https://codepen.io/team/Vue/pen/6c01922c9af3883890fd7393e8147ec4/">Fourth Step Axios and Vue</a> by Vue (<ahref="https://codepen.io/Vue">@Vue</a>) on <ahref="https://codepen.io">CodePen</a>.</p>
This can be even further improved with the use of components for different sections and more distinct error reporting, depending on the API you're using and the complexity of your application.
167
+
Ini bahkan dapat lebih ditingkatkan dengan penggunaan komponen untuk bagian-bagian yang berbeda dan pelaporan kesalahan yang lebih jelas, tergantung pada API yang Anda gunakan dan kompleksitas aplikasi Anda.
168
168
169
-
## Alternative Patterns
169
+
## Pola Alternatif
170
170
171
171
### Fetch API
172
172
173
-
The [Fetch API](https://developers.google.com/web/updates/2015/03/introduction-to-fetch)is a powerful native API for these types of requests. You may have heard that one of the benefits of the Fetch API is that you don't need to load an external resource in order to use it, which is true! Except... that it's not fully supported yet, so you will still need to use a polyfill. There are also some gotchas when working with this API, which is why many prefer to use axios for now. This may very well change in the future though.
173
+
[Fetch API](https://developers.google.com/web/updates/2015/03/introduction-to-fetch)adalah API _native_ yang bagus untuk jenis permintaan ini. Anda mungkin pernah mendengar bahwa salah satu keuntungan dari Fetch API adalah Anda tidak perlu memuat sumber daya luar untuk menggunakannya, dan itu benar! Tetapi... Fetch API belum di dukung secara penuh, jadi Anda tetap akan perlu menggunakan polyfill. Ada beberapa _gotcha_ saat bekerja dengan API ini, itulah sebabnya banyak orang lebih suka menggunakan axios untuk saat ini. Namun hal ini sangat mungkin untuk berubah di masa depan.
174
174
175
-
If you're interested in using the Fetch API, there are some [very good articles](https://scotch.io/@bedakb/lets-build-type-ahead-component-with-vuejs-2-and-fetch-api)explaining how to do so.
175
+
Jika anda tertarik menggunakan Fetch API, ada beberapa [artikel bagus](https://scotch.io/@bedakb/lets-build-type-ahead-component-with-vuejs-2-and-fetch-api)yang menjelaskan cara menggunakannya.
176
176
177
-
## Wrapping Up
177
+
## Ringkasan
178
178
179
-
There are many ways to work with Vue and axios beyond consuming and displaying an API. You can also communicate with Serverless Functions, post/edit/delete from an API where you have write access, and many other benefits. Due to the straightforward integration of these two libraries, it's become a very common choice for developers who need to integrate HTTP clients into their workflow.
179
+
Ada banyak cara untuk bekerja dengan Vue dan axios selain mengkonsumsi dan menampilkan API. Anda juga dapat berkomunikasi dengan _Serverless Functions_, memposting/mengedit/menghapus dari API dimana Anda memiliki hak akses tulis, dan masih banyak lagi manfaatnya. Karena integrasi yang mudah dari kedua pustaka ini, menjadikannya pilihan yang sangat umum bagi pengembang yang memerlukan integrasi _HTTP clients_ ke dalam alur kerja mereka.
0 commit comments