vue2和vue3配置全局自定义参数及vue3动态绑定ref
2024-10-21 03:40:35
在 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>
最新文章
- Spark作业调度阶段分析
- Linux学习之CentOS--CentOS6.下Mysql数据库的安装与配置
- pycharm 常用设置,打开文件数量
- php笔试题(1)--转载
- C# 将\u1234类型的字符转化成汉字
- Sea.js入门
- JavaScript之数据类型讲解
- csu oj 1811: Tree Intersection (启发式合并)
- Jquery插件收集
- ARM--存储管理器
- 双向队列 STL
- Android更改checkbox的style
- 从零开始制作 Hexo 主题
- 基本 SQL 之增删改查(二)
- jvm学习笔记二(减少GC开销的建议)
- 《linux就该这么学》第十六节课:第16,17章,Squid服务和iscsi网络存储
- python 备忘
- date命令以及date -d使用
- Struts2,springMVC获取request和response
- RSA,JAVA私钥加密,C#公钥解密
热门文章
- 垃圾收集器必问系列—CMS
- Unity之语音识别
- 淘宝数据采集之js采集
- 在执行npm install执行报错node-sass
- 【CodeSmith】The System.Data.SQLite library is not installed on this computer,不能使用SQLite解决办法
- VS 撰写生成了多个撰写错误,其根本原因有X点,如下所列。有关详细信息,请查看CompositionException.Error属性
- SpringMVC的常用注解、参数绑定、转发与重定向
- JZOJ 4250.路径
- 浅拷贝导致的bug
- dotnet总结