File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed
Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -35,3 +35,53 @@ methods: {
3535 }
3636}
3737```
38+
39+ ### Cancel token
40+
41+ You can cancel a request using a _ cancel token_ .
42+
43+ > The axios cancel token API is based on the withdrawn [ cancelable promises proposal] ( https://github.com/tc39/proposal-cancelable-promises ) .
44+
45+ You can create a cancel token using the ` CancelToken.source ` factory as shown below:
46+
47+ ``` js
48+ const source = this .$axios .CancelToken .source ()
49+
50+ this .$axios .$get (' /user/12345' , {
51+ cancelToken: source .token
52+ }).catch (error => {
53+ if (this .$axios .isCancel (error)) {
54+ console .log (' Request canceled' , error)
55+ } else {
56+ // handle error
57+ }
58+ })
59+
60+ this .$axios .$post (' /user/12345' , {
61+ name: ' new name'
62+ }, {
63+ cancelToken: source .token
64+ })
65+
66+ // cancel the request (the message parameter is optional)
67+ source .cancel (' Operation canceled by the user.' )
68+ ```
69+
70+ You can also create a cancel token by passing an executor function to the ` CancelToken ` constructor:
71+
72+ ``` js
73+ const { CancelToken } = this .$axios
74+ let cancel
75+
76+ this .$axios .$get (' /user/12345' , {
77+ cancelToken: new CancelToken (c => {
78+ // An executor function receives a cancel function as a parameter
79+ cancel = c
80+ }),
81+ })
82+
83+ // cancel the request
84+ cancel ()
85+ ```
86+
87+ > Note: you can cancel several requests with the same cancel token.
Original file line number Diff line number Diff line change @@ -184,6 +184,8 @@ export default (ctx, inject) => {
184184
185185 // Create new axios instance
186186 const axios = Axios . create ( axiosOptions )
187+ axios . CancelToken = Axios . CancelToken
188+ axios . isCancel = Axios . isCancel
187189
188190 // Extend axios proto
189191 extendAxiosInstance ( axios )
You can’t perform that action at this time.
0 commit comments