添加axios
This commit is contained in:
parent
21160355cc
commit
743479f728
44
src/main.js
44
src/main.js
@ -4,7 +4,7 @@ import { createApp } from 'vue'
|
|||||||
import App from './App.vue'
|
import App from './App.vue'
|
||||||
import router from './router.js'
|
import router from './router.js'
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
// import isMobile from 'ismobilejs'
|
import isMobile from 'ismobilejs'
|
||||||
|
|
||||||
// 引入 Bootstrap CSS
|
// 引入 Bootstrap CSS
|
||||||
import 'bootstrap/dist/css/bootstrap.min.css'
|
import 'bootstrap/dist/css/bootstrap.min.css'
|
||||||
@ -15,18 +15,36 @@ 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') {
|
||||||
// // isMobile() 函数需要一个 user-agent 字符串作为参数
|
// isMobile() 函数需要一个 user-agent 字符串作为参数
|
||||||
// mobileDetectResult = isMobile(navigator.userAgent);
|
mobileDetectResult = isMobile(navigator.userAgent);
|
||||||
// } else {
|
} else {
|
||||||
// // 为服务器端渲染 (SSR) 或其他非浏览器环境提供回退
|
// 为服务器端渲染 (SSR) 或其他非浏览器环境提供回退
|
||||||
// // isMobile('') 会返回一个所有检测标志都为 false 的对象
|
// isMobile('') 会返回一个所有检测标志都为 false 的对象
|
||||||
// mobileDetectResult = isMobile('');
|
mobileDetectResult = isMobile('');
|
||||||
// }
|
}
|
||||||
|
app.config.globalProperties.$isMobile = mobileDetectResult;
|
||||||
// app.config.globalProperties.$isMobile = mobileDetectResult;
|
//请求Global
|
||||||
|
try {
|
||||||
|
const response = await axios.get('/api/Global');
|
||||||
|
app.config.globalProperties.$Global = response.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; // 示例:但不推荐
|
||||||
|
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')
|
app.use(router).mount('#app')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user