0
0

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 1 year has passed since last update.

【Nuxt.js】axios-moduleでエラーハンドリングを共通化する

Posted at

概要

axios-module(@nuxtjs/axios)を使用。

axiosで非同期通信後のエラーハンドリングを共通化したかった。
ここではInterceptorsというものを使っていきます。

手順

1.pluginsディレクトリにエラーハンドリング用のファイルを作成

今回はわかりやすくaxios.jsとしますが、特になんでも大丈夫です。

axios.js
export default function ({ store, $axios, redirect }) {
  $axios.onError((error) => {
    // ここに処理を書いていく
    if (error.response.status === 400) {
      alert('Bad Responseです。');
    }
  });
}

2.nuxt.config.jsに追加したプラグインを設定する

pluginsに以下の通り、追加します。
ここに追加することでレスポンスが400で返ってくるとアラート画面が表示されるようになります。

nuxt.config.js
plugins: ['~/plugins/axios.js'],

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?