代码示例:
- import { method } from '@/utils/method'
- const app = createApp(App)
- // 全局方法挂载
- app.config.globalProperties.method= method
-
- // 调用-setup模式
- const { proxy } = getCurrentInstance();
- const result=proxy.method(args);
说明:
①、getCurrentInstance()在开发环境以及生产环境下都能到组件上下文对象;
②、setup的执行时组件对象还没有创建,此时不能使用this来访问data/computed/methods/props等属性或方法
可通过 getCurrentInstance这个函数来返回当前组件的实例对象,即当前vue这个实例对象。