VUE 3 使用 axios


<script lang="ts">
import {defineComponent} from 'vue';//导入defineComponent函数可以对使用$data调用data里的变量
import axios from "axios";//导入axios 可直接使用axios 
export default defineComponent({

  data() {
    return {
      unit: '',
    }
  },

  name: 'Home',

  methods: {

    save:function (){

      axios
          .post('http://localhost:8080/units', {
            //变量值必须加this.$data关键字才能正确对应data里面的对象
            unit_name: this.$data.unit, // 参数 unit_name 为后台对应变量名
          })
          .then(function (response) {
            //此处用的写法可保证进行成功回调时能执行其他代码
            if(response.status==201){
              alert("添加成功");
            }else{
              alert("添加失败");
            }

          })
          .catch(function (error) { // 请求失败处理
            console.log(error);
          });

    }

    }

});





</script>

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM