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