<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>