在 Vue2.x 中我们可以通过 Vue.prototype 添加全局属性 property。但是在 Vue3.x 中需要将 Vue.prototype 替换为 config.globalProperties 配置:

// Vue2.x
Vue.prototype.$api = axios;
Vue.prototype.$eventBus = eventBus; // Vue3.x
const app = createApp({})
app.config.globalProperties.$api = axios;
app.config.globalProperties.$eventBus = eventBus;

使用时需要先通过 vue 提供的 getCurrentInstance方法获取实例对象:

// A.vue

<script setup lang="ts">
import { ref, onMounted, getCurrentInstance } from "vue"; onMounted(() => {
const instance = <any>getCurrentInstance();
const { $api, $eventBus } = instance.appContext.config.globalProperties;
// do something
})
</script>

vue3动态绑定ref

<div v-for="element in childApp" :key="element.key" class="appWrap" :ref="setChildAppRef">
{{element.content}}
</div>
<script setup>
let childAppRefs = []
let childApp = computed(() => store.state.childApp)
const setChildAppRef = (el) => {
if (el) {
childAppRefs.push(el)
}
}
</script>

最新文章

  1. Spark作业调度阶段分析
  2. Linux学习之CentOS--CentOS6.下Mysql数据库的安装与配置
  3. pycharm 常用设置,打开文件数量
  4. php笔试题(1)--转载
  5. C# 将\u1234类型的字符转化成汉字
  6. Sea.js入门
  7. JavaScript之数据类型讲解
  8. csu oj 1811: Tree Intersection (启发式合并)
  9. Jquery插件收集
  10. ARM--存储管理器
  11. 双向队列 STL
  12. Android更改checkbox的style
  13. 从零开始制作 Hexo 主题
  14. 基本 SQL 之增删改查(二)
  15. jvm学习笔记二(减少GC开销的建议)
  16. 《linux就该这么学》第十六节课:第16,17章,Squid服务和iscsi网络存储
  17. python 备忘
  18. date命令以及date -d使用
  19. Struts2,springMVC获取request和response
  20. RSA,JAVA私钥加密,C#公钥解密

热门文章

  1. 垃圾收集器必问系列—CMS
  2. Unity之语音识别
  3. 淘宝数据采集之js采集
  4. 在执行npm install执行报错node-sass
  5. 【CodeSmith】The System.Data.SQLite library is not installed on this computer,不能使用SQLite解决办法
  6. VS 撰写生成了多个撰写错误,其根本原因有X点,如下所列。有关详细信息,请查看CompositionException.Error属性
  7. SpringMVC的常用注解、参数绑定、转发与重定向
  8. JZOJ 4250.路径
  9. 浅拷贝导致的bug
  10. dotnet总结