最近我完成了本教程:https://github.com/Hujjat/laravStart 在这个应用程序中,我可以成功运行vuejs应用程序和axios.then.catch.finally或this.form.then.catch.finally 但我在最近升级到laravel 5.7的旧项目中添加了npm。 在我的应用程序中,我无法最终添加功能。如果我这样做,它说:
未捕获的TypeError:axios.post(...)。then(...)。catch(...)。最终是 不是功能
我的app.js:
/**
* First we will load all of this project's JavaScript dependencies which
* includes Vue and other libraries. It is a great starting point when
* building robust, powerful web applications using Vue and Laravel.
*/
// require('./bootstrap');
import Vue from 'vue'
window.Vue = Vue
window.axios = require('axios');
import { Form, HasError, AlertError } from 'vform'
window.Form = Form;
Vue.component(HasError.name, HasError)
Vue.component(AlertError.name, AlertError)
Vue.component('pagination', require('laravel-vue-pagination'));
//select 2
import vSelect from 'vue-select'
Vue.component('v-select', vSelect)
//sweet alert 1
import swal1 from 'sweetalert';
window.swal1 = swal1;
import swal from 'sweetalert2'
window.swal = swal;
const toast = swal.mixin({
toast: true,
position: 'top-end',
showConfirmButton: false,
timer: 3000
});
window.toast = toast;
/**
* Next, we will create a fresh Vue ap
*
*
* plication instance and attach it to
* the page. Then, you may begin adding components to this application
* or customize the JavaScript scaffolding to fit your unique needs.
*/
Vue.component('example-component', require('./components/ExampleComponent.vue'));
Vue.component('timetable-component', require('./components/TimetableComponent.vue'))
/* filter active and inactive */
Vue.filter('activeInactive', function (value) {
if(value==1){
return 'Active'
}else{
return 'Inactive'
}
})
const app = new Vue({
el: '#app',
data:{
},
methods:{
},
mounted(){
}
});
我认为我的app.js与表单请求的教程app.js完全相同。 另一件事是,在本教程中,他没有在任何地方使用axios import,但他使用得很流畅。但是如果没有导入,我将无法使用axios.then .... 他如何在不导入axios的情况下使用? 我如何才能最终与then.catch一起使用?
一个组件网:
loadSite() {
axios
.get("/api/site/list")
.then(({ data }) => {
console.log(data);
this.siteList = data;
})
.catch(function(error) {
console.log(error);
}).finally(()=>{
});
},
答案 0 :(得分:4)
作为document,要使finally
有效,您需要添加promise.prototype.finally
npm install axios promise.prototype.finally --save
然后
const axios = require('axios');
require('promise.prototype.finally').shim();
axios
.get('http://www.example.com/user')
.then((response) => {
console.log(response.data);
return response;
})
.finally(() => {
console.log('this will always be called');
});
答案 1 :(得分:0)
我有同样的问题,只需添加此文件即可解决
<script src="https://polyfill.io/v3/polyfill.min.js?features=Promise.prototype.finally" defer></script>