页面加载时自动获取数据 - daniel-qa/Vue GitHub Wiki
onMounted()
<script setup>
import { ref, computed, onMounted } from "vue";
import axios from "axios";
// 获取学区和学校数据
const fetchData = async () => {
const requestData = { showSchool: true };
try {
const response = await axios.post(
"https://localhost:5001/notice/get-areas",
requestData
);
const { state, result } = response.data;
if (state === 200) {
// 更新父级菜单(学区)
items.value = result.map((area) => area.name);
// 更新子级菜单(学校)
subItems.value = result.map((area) =>
area.schools.map((school) => school.name)
);
ElMessage.success("学区信息获取成功!");
} else {
ElMessage.error("获取学区数据失败,请稍后重试!");
}
} catch (error) {
console.error("请求失败:", error);
ElMessage.error("网络错误,请检查后重试!");
}
};
// 页面加载时自动获取数据
onMounted(fetchData);
</script>