解决请求问题
This commit is contained in:
parent
17cd6886bf
commit
2fd084ee85
39
src/main.js
39
src/main.js
@ -15,6 +15,7 @@ const app = createApp(App)
|
||||
axios.defaults.baseURL = 'https://cms2.yangprivate.site'
|
||||
// 全局挂载 axios,所有组件都可以 this.$axios 访问
|
||||
app.config.globalProperties.$axios = axios
|
||||
|
||||
// 安全地获取 User Agent 并进行检测
|
||||
let mobileDetectResult;
|
||||
if (typeof navigator !== 'undefined') {
|
||||
@ -26,25 +27,31 @@ if (typeof navigator !== 'undefined') {
|
||||
mobileDetectResult = isMobile('');
|
||||
}
|
||||
app.config.globalProperties.$isMobile = mobileDetectResult;
|
||||
//请求Global
|
||||
|
||||
// 使用 async IIFE 来处理顶层 await
|
||||
(async () => {
|
||||
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
|
||||
//请求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 response = await axios.get('/api/About');
|
||||
// app.config.globalProperties.$About = response.data.data; // 示例:但不推荐
|
||||
// console.log('Data fetched:', response.data);
|
||||
// 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' };
|
||||
// app.config.globalProperties.$About = { error: 'Failed to load About data' };
|
||||
// }
|
||||
|
||||
app.use(router).mount('#app')
|
||||
} 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');
|
||||
}
|
||||
})();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user