vue.js3: 使用全局变量(vue@3.2.37)
一,js代码:
1,main.js
import { createApp } from 'vue' import App from './App.vue' import router from './route' //判断是否移动端的函数 const isMobileFunc = () => { let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i); if (flag === null) { return 0; } else { return 1; } }; const app = createApp(App) app.use(router) // 定义全局变量:平台 if (isMobileFunc() == 1) { app.config.globalProperties.$platForm= "mobile"; } else { app.config.globalProperties.$platForm= "pc"; } // 定义全局变量:浏览器的宽高 app.config.globalProperties.$screenSize={ width:0, height:0, }; app.mount('#app')
2,App.vue
<template> <router-view /> </template> <script> import {onMounted} from "vue"; import {getCurrentInstance} from 'vue' export default { name: 'App', setup() { onMounted(()=>{ let globalProperties = getCurrentInstance().appContext.config.globalProperties; //得到浏览器的宽高 let screen = { width:document.documentElement.clientWidth, height: document.documentElement.clientHeight, }; globalProperties.$screenSize = screen; //窗口改变大小时 window.onresize = () => { let screen = { width:document.documentElement.clientWidth, height: document.documentElement.clientHeight, }; globalProperties.$screenSize = screen; } }) } } </script> <style> html,body{ margin:0; padding:0; } #app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; /*margin-top: 60px;*/ } </style>
3,Home.vue
<template> <div> <router-link :to="{ path: '/code/code', query: { codeId: 123 }}"> 去code页面 </router-link> platform:{{platForm}}<br/> screen width:{{$screenSize.width}}<br/> screen height:{{$screenSize.height}}<br/> </div> </template> <script> import {getCurrentInstance,ref} from 'vue' export default { name: "HomePage", setup() { let globalProperties = getCurrentInstance().appContext.config.globalProperties; const platForm = ref(globalProperties.$platForm); const screenSize= ref(globalProperties.$screenSize); return { platForm, screenSize, } } } </script> <style scoped> </style>
说明:刘宏缔的架构森林是一个专注架构的博客,
网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/06/02/vue-js3-shi-yong-quan-ju-bian-liang-vue-3-2-37/
对应的源码可以访问这里获取: https://github.com/liuhongdi/
或: https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,测试效果
三,查看vue框架的版本:
liuhongdi@lhdpc:/data/vue/child$ npm list vue child@0.1.0 /data/vue/child ├─┬ @vue/cli-plugin-babel@5.0.6 │ └─┬ @vue/babel-preset-app@5.0.6 │ └── vue@3.2.37 deduped ├─┬ vue-router@4.1.2 │ └── vue@3.2.37 deduped └─┬ vue@3.2.37 └─┬ @vue/server-renderer@3.2.37 └── vue@3.2.37 deduped
分类:
vue.js
标签:
javascript
, js
, vue
, vue.js
, vue3
, 全局变量
, getCurrentInstance
, appContext
, globalProperties
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2020-07-20 spring boot:多个filter/多个interceptor/多个aop时设置调用的先后顺序(spring boot 2.3.1)