LoginSignup
3
1

More than 3 years have passed since last update.

nuxtのmiddlewareメモ

Posted at

middlewareとは

このライフサイクルの最初の方にあるもの。レンダリングする前に処理を行いたいときに使う。また全ページ共通の処理としてもかけるので共通のものを書きたいときにも使う。

書き方

  • middleware/ ディレクトリに書く
  • middleware/auth.js は auth ミドルウェアになる
  • ミドルウェアは第一引数として コンテキスト を受け取ります
  • 下記のように
nuxt.config.js
export default {
  router: {
    middleware: 'auth'
  }
}
middleware/stats.js
import axios from 'axios'

export default function ({ route }) {
  return axios.post('http://my-stats-api.com', {
    url: route.fullPath
  })
}
3
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
3
1