LoginSignup
13
14

More than 3 years have passed since last update.

Vue.jsで、何かの処理後にリロードする

Last updated at Posted at 2019-09-12

Vue.jsで、何かの処理後にリロードする

参考にしました。
https://qiita.com/CeMoReOn/items/4ce640bee42bf1a0cbd7

axiosでRailsにPOSTしてGoogleカレンダーにイベントを登録したが、ページ遷移しないのでカレンダーに反映されない。

リロードすると反映されるので意図的にリロードしたい。

vue-routerの機能を使うと出来る。

肝はこれ。

this.$router.go({path: this.$router.currentRoute.path, force: true})

1.
$router.goを使用。

2.
引数のpathに、遷移したいパスを指定する。
今回は現在のパス(this.$router.currentRoute.path)を渡す。
すると、現在のパスに遷移してくれる。

以下のようにパスを直指定しても同様に動きました。


this.$router.go({path: '/calendars', force: true})

最終的に以下のようになりました。

calendar.vue
   methods: {
      createEvent: function() {
        axios
        .post('/calendars',
          {
            calendar: {
              start: this.calendar.start,
              end: this.calendar.end,
              summary: this.calendar.summary,
              description: this.calendar.description
            }
          }
        ).then((res) => {
          console.log(`Success... ${res.data}`),
          this.calendar.start = '',
          this.calendar.end = '',
          this.calendar.summary = '',
          this.calendar.description = '',
          this.$router.go({path: this.$router.currentRoute.path, force: true})
        }, (error) => {
          console.log(error)
        })
      }
    }

これでPOST後にリロードされ、欲しい挙動になった。

13
14
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
13
14