finally() 在 Vuejs 另一个应用程序中不起作用

Joh*_*ohn 0 vue.js laravel-5 axios

最近我做了这个教程。

在这个应用程序中,我可以成功运行 vuejs 应用程序和axios.then.catch.finallythis.form.then.catch.finally.

但是我在我最近升级到 Laravel 5.7 的旧项目中添加了 npm。在我的应用程序中,我无法添加finally功能。如果我这样做,它说:

未捕获的类型错误:axios.post(...).then(...).catch(...).finally 不是函数

我的 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(){

    }
});
Run Code Online (Sandbox Code Playgroud)

我认为我的 app.js 与用于表单请求的教程 app.js 完全相同。另一件事是,在教程中他没有在任何地方使用 axios import 但他很顺利地使用了它。但是没有导入,我不能使用axios.then. 不导入axios他是怎么用的?我如何使用finallythen.catch

一个组件:

loadSite() {
  axios
    .get("/api/site/list")
    .then(({ data }) => {
      console.log(data);
      this.siteList = data;
    })
    .catch(function(error) {
      console.log(error);
    }).finally(()=>{ 

    });
},
Run Code Online (Sandbox Code Playgroud)

itt*_*tus 6

作为文档,要制作finally作品,您需要添加promise.prototype.finally

npm install axios promise.prototype.finally --save
Run Code Online (Sandbox Code Playgroud)

进而

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');
  });
Run Code Online (Sandbox Code Playgroud)