vue3
在main.js中挂载

// main.js

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

const flag = function IsPC() {
	var userAgentInfo = navigator.userAgent
	var Agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad']
	var flag = true
	for (var v = 0; v < Agents.length; v++) {
		if (userAgentInfo.indexOf(Agents[v]) > 0) {
			flag = false
			break
		}
	}
	return flag
}

// 必须给createdApp(App)赋值,如果直接写createApp(App).config挂载的话会报错,至于为啥,我不想知道
const app = createApp(App)  
// 挂载到原型用config.globalProperties
app.config.globalProperties.$flag = flag
app.use(store).use(router).use(vant).use(ElementPlus).mount('#app')

在setup中调用

<script>
// 必须引入
import { getCurrentInstance } from 'vue'

export default {
	name: 'travelTravel',
	setup() {
		const { proxy } = getCurrentInstance()
		console.log(proxy.$flag())   // 此时就能打印return 的flag的值
	},
}
</script>
<style lang="scss" scoped></style>
Logo

前往低代码交流专区

更多推荐