uni-app Vue3 添加全局变量及全局方法
uni-app Vue About 1,495 words添加全局变量
在App.vue
中添加globalData
变量
<script>
let host = 'https://product.com';
uni.getSystemInfoSync({
success: function(res) {
if (res.uniPlatform === 'web') {
host = 'https://test.com';
}
}
});
export default {
globalData: {
host: host
},
}
</script>
添加全局方法
在main.js
中,添加app.config.globalProperties
。
import App from './App'
// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
...App
})
app.$mount()
// #endif
// #ifdef VUE3
import {
createSSRApp
} from 'vue'
function getDate(date, AddDayCount = 0) {
if (!date) {
date = new Date()
}
if (typeof date !== 'object') {
date = date.replace(/-/g, '/')
}
const dd = new Date(date)
dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期
const y = dd.getFullYear()
const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期,不足10补0
const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号,不足10补0
return {
fullDate: y + '-' + m + '-' + d,
year: y,
month: m,
date: d,
day: dd.getDay()
}
}
export function createApp() {
const app = createSSRApp(App)
app.config.globalProperties.getDate = getDate;
return {
app
}
}
// #endif
参考
Views: 34 · Posted: 2025-07-17
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓

Loading...