LoginSignup
10
7

More than 5 years have passed since last update.

vue.jsでフォームを離脱するときに警告をだすやつを実装する

Posted at

最近良く見る、フォームを離脱するときに出る警告をvue.jsで実装してみました。
スクリーンショット 2018-11-22 13.49.05.png

ナビゲーションガードのbeforeRouteLeaveを使うとできます。
beforeRouteLeaveは、このコンポーネントを描画するルートが間もなくナビゲーションから離れていく時に呼ばれます。

beforeRouteLeave (to, from , next) { 
    const answer = window.confirm('編集した内容が消えちゃうけど移動しますか?(´・ω・`)') |
    if (answer) { 
        next() 
    } else { 
        next(false)
    }
},

これだけで実装できちゃうので簡単ですね。

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