解决请求问题

This commit is contained in:
zhcnyuyang 2025-06-10 00:56:29 +08:00
parent 17cd6886bf
commit 2fd084ee85

View File

@ -15,6 +15,7 @@ const app = createApp(App)
axios.defaults.baseURL = 'https://cms2.yangprivate.site' axios.defaults.baseURL = 'https://cms2.yangprivate.site'
// 全局挂载 axios所有组件都可以 this.$axios 访问 // 全局挂载 axios所有组件都可以 this.$axios 访问
app.config.globalProperties.$axios = axios app.config.globalProperties.$axios = axios
// 安全地获取 User Agent 并进行检测 // 安全地获取 User Agent 并进行检测
let mobileDetectResult; let mobileDetectResult;
if (typeof navigator !== 'undefined') { if (typeof navigator !== 'undefined') {
@ -26,25 +27,31 @@ if (typeof navigator !== 'undefined') {
mobileDetectResult = isMobile(''); mobileDetectResult = isMobile('');
} }
app.config.globalProperties.$isMobile = mobileDetectResult; app.config.globalProperties.$isMobile = mobileDetectResult;
//请求Global
try {
const response = await axios.get('/api/Global?populate=*');
app.config.globalProperties.$Global = response.data.data; // 示例:但不推荐
console.log('Data fetched:', response.data);
} catch (error) {
console.error('Failed to fetch initial data:', error);
// 可以设置一个默认值或错误状态
app.config.globalProperties.$Global = { error: 'Failed to load' };
}
//请求About
// try {
// const response = await axios.get('/api/About');
// app.config.globalProperties.$About = response.data.data; // 示例:但不推荐
// console.log('Data fetched:', response.data);
// } catch (error) {
// console.error('Failed to fetch About data:', error);
// // 可以设置一个默认值或错误状态
// app.config.globalProperties.$About = { error: 'Failed to load' };
// }
app.use(router).mount('#app') // 使用 async IIFE 来处理顶层 await
(async () => {
try {
//请求Global
const globalResponse = await axios.get('/api/Global?populate=*');
app.config.globalProperties.$Global = globalResponse.data.data;
console.log('Global data fetched:', globalResponse.data);
// 请求About (如果您需要启用这部分,取消注释即可)
// try {
// const aboutResponse = await axios.get('/api/About');
// app.config.globalProperties.$About = aboutResponse.data.data;
// console.log('About data fetched:', aboutResponse.data);
// } catch (error) {
// console.error('Failed to fetch About data:', error);
// app.config.globalProperties.$About = { error: 'Failed to load About data' };
// }
} catch (error) {
console.error('Failed to fetch initial global data:', error);
// 如果 $Global 获取失败,可以设置一个默认值或错误状态
app.config.globalProperties.$Global = { error: 'Failed to load Global data' };
} finally {
// 确保在所有异步初始化操作尝试完毕后挂载应用
app.use(router).mount('#app');
}
})();