Let Vue methods support promise
npm install --save vue-methods-promise
import Vue from 'vue'
import vueMethodsPromise from 'vue-methods-promise'
Vue.use(vueMethodsPromise, {
hookName: '$promise', // Component default hook name
promise: (mp) => { // Promise callback
mp
.catch(function (err) {
console.log(mp)
})
}
})
<div id="app"></div>
<script src="https://cdn.jsdelivr.net/vue/latest/vue.js"></script>
<script src="./dist/vue-methods-promise.js"></script>
<script>
vueMethodsPromise(Vue, {
hookName: '$promise', // Component default hook name
promise: function (mp) { // Promise callback
mp
.catch(function (err) {
console.log(mp)
})
}
})
new Vue({
el: '#app',
mounted: function () {
this.init()
},
methods: { //
init: function () {
return new Promise(function (resolve, reject) {
setTimeout(reject, 500)
})
},
$promise (mp) { // Optional parameters. Component hook function
return mp.then(function () {
//
})
}
}
})
</script>