我无法在Axios Post操作vueJs之后集成Sweet Alert

时间:2018-11-06 23:17:20

标签: vue.js

我只想在执行axios post时集成此烤面包。我就这样放置它,即使我的post axios函数不起作用,它也会显示出来。

我该如何解决?

我的代码:

   methods: {
     addRinvoice: function () {
       const toast = swal.mixin({
      toast: true,
      position: 'top-end',
      showConfirmButton: false,
      timer: 9000
      });

      axios.post('/addrinvoice', this.rinvoice)
            .then(response => {



      toast({
      type: 'success',
      title: 'Invoice added in Database'
       }),


            console.log(response.data);
             if (response.data.etat) {
             this.rinvoice = {
             id: 0,
             amount: response.data.etat.amount,
             };}})

            .catch(error => {
                console.log('errors: ', error)
             }) },

2 个答案:

答案 0 :(得分:1)

只需将调用toast方法的调用放入then()方法中即可:

  methods: {
     addRinvoice: function () {

       axios.post('/addrinvoice', this.rinvoice)
        .then(response => {
           const toast = swal.mixin({
             toast: true,
             position: 'top-end',
             showConfirmButton: false,
             timer: 9000
            });

         toast({
             type: 'success',
             title: 'Invoice added in Database'
         }),
         console.log(response.data);
         if (response.data.etat) {
           this.rinvoice = {
           id: 0,
           amount: response.data.etat.amount,
          };}})

        .catch(error => {
            console.log('errors: ', error)
         }) 

       },

答案 1 :(得分:0)

成功获得响应后,将其放置在then内,

then(response=>{
     ...
     const toast = swal.mixin({
        toast: true,
        position: 'top-end',
       showConfirmButton: false,
       timer: 9000
       });

       toast({
           type: 'success',
           title: 'Invoice added in Database'
        }
       }