LoginSignup
1
1

More than 3 years have passed since last update.

【Vue.js】CodePenでVue.jsを実装する方法とaxiosでAPIリクエストやってみた

Last updated at Posted at 2019-08-28

2019.08.28時点

Vue.js サンプル axiosでAPIリクエスト


See the Pen
nonVuejsAxiosAPI
by noriko fujita (@nonkapibara)
on CodePen.




■CodePenのサイト
https://codepen.io/

1.CodePenにログインし、「Pen」を選択する
スクリーンショット 2019-08-28 19.57.23.png

2.「Settings」を選択する
スクリーンショット 2019-08-28 19.58.01.png

3.「JavaScript」を選択する
スクリーンショット 2019-08-28 19.58.47.png

4.Vue.jsのCDNを追加する
「Add External Scripts/Pens」のサーチエリアに「vue」と入力する
スクリーンショット 2019-08-28 20.00.16.png

vueを選択する
スクリーンショット 2019-08-28 20.01.04.png

こんな感じで、Vue.jsのCDNが追加される。
スクリーンショット 2019-08-28 22.16.51.png

5.APIリクエストで「axios」を使用する場合は、
「Add External Scripts/Pens」のサーチエリアに「axios」と入力する
スクリーンショット 2019-08-28 22.03.34.png

6.axiosを選択する
スクリーンショット 2019-08-28 22.04.17.png

7.保存する
スクリーンショット 2019-08-28 22.04.50.png

8.HTMLにdivから書く事ができる。

index.html
  <div id="app">
    {{ info }}
  </div>
<script src="./main.js"></script>
main.js
new Vue({
  el: '#app',
  data() {
    return {
      info: null
    }
  },
  mounted () {
    axios
      .get('https://api.coindesk.com/v1/bpi/currentprice.json',{ responseType: 'arraybuffer' })
      .then(response => (this.info = response))
  }
})

スクリーンショット 2019-08-28 22.27.02.png

Fullだとこんな感じ

index.html
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Vue.js API</title>
    <!-- 開発バージョン -->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>

<body>
  <div id="app">
    {{ info }}
  </div>
    <script src="./main.js"></script>
</body>

</html>

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1