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
复制代码

 

posted @   刘宏缔的架构森林  阅读(1603)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有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)
点击右上角即可分享
微信分享提示