• vue3第二十三节(全局属性方法应用)


    vue2 与 vue3 的全局属性使用方法区别
    1、globalProperties getcurrentinstace vue3 中已经移除对外暴露 getcurrentinstace,建议使用下面两种
    2、provide | inject
    3、mitt 事件总线程

    1、vue2 通过 prototype 实例上挂载属性/方法,用于全局调用

    // main.js
    import Vue from 'vue'
    import App from './App.vue'
    Vue.prototype.$message = '测试'
    Vue.mount('#app',App')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    // 使用
    methods:{
      test(){
        // 直接通过 this.$message 即可实现调用
        console.log(this.$message)
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2、vue3 中建议通过 provide | inject 挂载全局属性/方法

    // main.js
    
    import { createApp } from 'vue'
    import App from './App.vue'
    
    const app = createApp(App)
    app.config.globalProperties.$message = '测试' // 这种方案vue3中已不建议使用
    app.provide('message', '测试')
    app.mount('#app')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3.1、通过 provide | inject 挂载全局属性/方法 建议使用

    main.ts
    通过全局依赖,如:app.provide('message', '测试') 这样即可在组件中通过 inject 获取到全局属性;

    <script setup>
    const message = inject('message')
    console.log(message) // 测试
    </script>
    
    • 1
    • 2
    • 3
    • 4

    3.2、通过globalProperties 挂载全局属性/方法 不建议使用

    该属性发现在 vue3 的api文档中已经无法搜索到,已被隐藏,不建议使用
    app.config.globalProperties定义的全局变量不支持双向绑定,只是声明了一个全局的变量

    // main.ts
    app.config.globalProperties.$message = '测试'
    
    • 1
    • 2
    使用时
    <script setup>
    import { getcurrentInstance } from 'vue'
    getcurrentInstance.appContext.config.globalProperties.$message // 测试
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3.3、事件总线 mitt() 不建议使用

     yarn add mitt
    
    • 1
     // main.ts
     import mitt from "mitt"
     const Mitt = mitt()
     // 声明
     declare module 'vue'{
      export interface ComponentCustomProperties{
        $Bus: typeof Mitt
      }
    }
     app.config.globalProperties.$Bus = Mitt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    使用时候
    使用emit()派发事件 on()接受事件 off()事件移除 clear()情况所有

    <script setup>
    import {getCurrentInstance} from "vue"
    const instance = getCurrentInstance().appContext.config.globalProperties
    // 派发事件
    instance.proxy.$Bus.emit("on-changeParams","修改参数")
    // 监听事件
    
    instance.proxy.$Bus.on("on-changeParams",(params) => {
      console.log('--params-')
    })
    // 关闭事件
    instance.proxy.$Bus.off("on-changeParams",(params) => {
      console.log('--params-')
    })
    //清空所有监听
    instance.all.clear();
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    vue3 中 3.3版本以上 组合式 API 中建议 使用 provide() | inject() 依赖注入的方法进行全局属性方法挂载

  • 相关阅读:
    手写Mybatis源码(原来真的很简单!!!)
    【Spring源码】9. 重要的ConfigurationClassPostProcessor
    自动化测试selenium基础篇——webdriverAPI
    redis -- 数据类型及操作
    【华为OD机试真题 python】n进制减法 【2022 Q4 | 200分】
    图像倾斜角度求取-Radon变换
    reinterpret_cast和static_cast转换的区别
    算法_每日一题(9.6)
    消息中间件介绍
    面向6G的无蜂窝大规模MIMO无线传输技术
  • 原文地址:https://blog.csdn.net/weixin_39593730/article/details/138199799