4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vue.jsでGETやPOSTを実装する際の非公式・公式ライブラリーについて

Last updated at Posted at 2018-07-28

概要

最近、仕事で必要に迫られVue.jsの学習を始めたところ、Vue.jsでPOSTやGETでリクエストを投げる際のライブラリーに非公式のものと公式のものがあることを知りました
( 今更感が凄いのはご愛嬌...)

非公式

vue-resource

vue-resourceの非公式化に関するリンク
https://jp.vuejs.org/2016/11/03/retiring-vue-resource/

AngularJSでGETやPOSTを実装する際に利用する $resource と同じ様に使えるイメージです。なお現在、こちらのライブラリーは、上記のリンクにあるように2016年以降 非推奨 のライブラリとなっています。

methods:{
 fetchData(){
   this.$http.get('api/hoge').then(response=>{
     何らかの処理
   }).error(function (data, status, request) {
     エラー処理
   })
 }
}

公式

axios

関連リンク
https://jp.vuejs.org/v2/cookbook/using-axios-to-consume-apis.html

こちらはVue.js公式のpromiseベースのHTTPクライアントとして 推奨 されているライブラリーとされています。
基本的にこちらを使うのが良さそうです。
(以下の例はTODOアプリを作った時のもの)

methods: {
 fetchTasks: function () {
   axios.get('/api/tasks').then((response) => {
      for (let i = 0; i < response.data.tasks.length; i++) {
        this.tasks.push(response.data.tasks[i])
      }
   }, (error) => {
      console.log(error);
   });
 },

最後に

本記事は自身のVue.js学習途中の備忘録として投稿させて頂きました!
無知なことから内容に誤りなどあることと思います。
もし投稿内容に不備がありましたら、お手数ですがコメント頂ければ幸いです :pray:

4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?