Skip to content
This repository was archived by the owner on Jul 10, 2019. It is now read-only.

Commit ac6ab7a

Browse files
committed
feat: add beforeRouteLeave and show alert when api succeed
1 parent dcb7def commit ac6ab7a

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/pages/example/api.vue

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,30 @@ import { IApiPayload } from '@/interfaces/api/IApiPayload'
1414
import { ApiInterface } from '@/store/api'
1515
import { cancelToken } from '@/utilities/'
1616
17-
@Component
17+
@Component({
18+
// https://router.vuejs.org/ja/guide/advanced/navigation-guards.html#%E3%83%AB%E3%83%BC%E3%83%88%E5%8D%98%E4%BD%8D%E3%82%AC%E3%83%BC%E3%83%89
19+
beforeRouteEnter(to, from, next) {
20+
// このコンポーネントを描画するルートが確立する前に呼ばれます。
21+
// `this` でのこのコンポーネントへのアクセスはできません。
22+
// なぜならばこのガードが呼び出される時にまだ作られていないからです!
23+
next()
24+
},
25+
beforeRouteUpdate(this: Api, to, from, next) {
26+
// このコンポーネントを描画するルートが変更されたときに呼び出されますが、
27+
// このコンポーネントは新しいルートで再利用されます。
28+
// たとえば、動的な引数 `/foo/:id` を持つルートの場合、`/foo/1` と `/foo/2` の間を移動すると、
29+
// 同じ `Foo` コンポーネントインスタンスが再利用され、そのときにこのフックが呼び出されます。
30+
// `this` でコンポーネントインスタンスにアクセスできます。
31+
next()
32+
},
33+
// このコンポーネントを描画するルートが間もなく
34+
// ナビゲーションから離れていく時に呼ばれます。
35+
// `this` でのコンポーネントインスタンスへのアクセスができます。
36+
beforeRouteLeave(this: Api, to, from, next): void {
37+
console.log('beforeRouteLeave', to, from, next)
38+
next()
39+
}
40+
})
1841
export default class Api extends Vue {
1942
// リクエスト用ペイロード
2043
private payload: IApiPayload = {
@@ -27,14 +50,13 @@ export default class Api extends Vue {
2750
}
2851
// @ts-ignore
2952
public async fetch({ store, params, error }: any): Promise<void> {
30-
await console.log('fetch')
31-
// await store.dispatch('api/fetchApi', {
32-
// hoge: 'foo'
33-
// })
53+
await store.dispatch('api/fetchApi', {
54+
hoge: 'foo'
55+
})
3456
}
3557
public async created() {
36-
console.log('created')
37-
await this.$store.dispatch('api/fetchApi', this.payload)
58+
await console.log('created')
59+
// await this.$store.dispatch('api/fetchApi', this.payload)
3860
}
3961
public beforeDestroy(): void {
4062
// リクエストをキャンセル

src/store/api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export const actions = {
8585
} as AxiosRequestConfig // https://github.com/axios/axios/blob/master/index.d.ts
8686
)
8787
console.log('apiResult:', apiResult)
88+
alert('fetchApi is done')
8889

8990
// 値をストアに保存
9091
commit('saveApiResult', apiResult)

0 commit comments

Comments
 (0)