改为 // 方式二,此方法在开发环境以及生产环境下都能放到组件上下文对象(推荐)constproxy =getCurrentInstance(); 使用://onMounted(async() => {if(proxy) {//挂载自己的函数函数,在其他地方进行使用proxy.onDownloadProgress= onDownloadProgress; } }) 如果要使用全局的挂载的组件:// 获取当前组件实例constinstance =getCurrentInstance(); instance.proxy...
二、getCurrentInstance使用注意点 1. getCurrentInstance 只能在 setup 或生命周期钩子中使用 举个例子: import{ getCurrentInstance, onMounted}from'vue'exportdefault{setup() {constrefName ='cxContainer'constonResize= () => {constinstance =getCurrentInstance()console.log('instance', instance) }onMounted(() =...
import { getCurrentInstance, ComponentInternalInstance } from "vue"; export default () => (getCurrentInstance() as ComponentInternalInstance).appContext.config.globalProperties; 页面使用: import useGlobalProperties from "@/hooks/useGlobalProperties"; const proxy = useGlobalProperties(); const onSubmit = ...
const{ ctx } =getCurrentInstance();// 方式一,这种方式只能在开发环境下使用,生产环境下的ctx将访问不到const{ proxy } =getCurrentInstance();// 方式二,此方法在开发环境以及生产环境下都能放到组件上下文对象(推荐)// ctx 中包含了组件中由ref和reactive创建的响应式数据对象,以及以下对象及方法;proxy.$attrs...
案例-getCurrentInstance注意 vue2 定义Vue.prototype.$utils=utils;Vue.prototype.$bus=newVue();使用this.$utilsthis.$bus vue3 定义 app.config.globalProperties.$utils=utils;app.config.globalProperties.$axios=getRequest;使用import{getCurrentInstance}from"vue";const{proxy}=getCurrentInstance();proxy.$utils ...
constarr=reactive([])arr.push(...arr2) 多数据源我们采用包裹方法 conststate=reactive({arr:[]});state.arr=arr2 那么对于上面的案例来说,就得改成如下: conststate=reactive({userLists:[]})constinternalInstance=getCurrentInstance()asyncfunctiongetUserList(){const{data:res}=awaithttp(internalInstance...
const{ proxy } =getCurrentInstance() AI代码助手复制代码 在ts中使用会报错:报错:...类型“ComponentInternalInstance | null” 我们在项目中一般会用到很多getCurrentInstance()方法,直接封装一下 创建useCurrentInstance.ts文件: import{ComponentInternalInstance, getCurrentInstance }from'vue'exportdefaultfunctionuseCurr...
import { getCurrentInstance } from 'vue'; // 获取当前组件实例 const instance = getCurrentInstance(); // 获取当前组件的上下文,下面两种方式都能获取到组件的上下文。 const { ctx } = getCurrentInstance(); // 方式一,这种方式只能在开发环境下使用,生产环境下的ctx将访问不到 const { proxy } = getCurr...
const { appContext } = getCurrentInstance() as ComponentInternalInstance const proxy = appContext.config.globalProperties return { proxy } } 1. 2. 3. 4. 5. 6. 7. 8. 组件内使用: import { defineComponent } from "vue"; import useCurrentInstance...
import{getCurrentInstance}from'vue';const{proxy}=getCurrentInstance();functiongo(){proxy.$router.push("/my02");} 这里的 proxy 就类似于以前 Vue2 中的 this。 松哥这里是以 router 为例来和大家演示,如果是 Vuex/Pinia,也有类似的写法,我就不挨个演示了。 无论是上面那种写法,都需要...