VUE3如何在组件setup外获取实例

组件setup内可以用getCurrentInstance,如果在router中做路由拦截的时候怎么获取呢?

阅读 4.3k
1 个回答

换个思路:

getCurrentInstance 只能在 setup 或生命周期钩子中调用。

如果封装了一个 $http,app.config.globalProperties.$http = () => {}
在setup 我们可以这样访问

setup() {
  const internalInstance = getCurrentInstance()
  internalInstance.appContext.config.globalProperties.$http
}

在 route.ts 文件或者其他文件,直接使用导入的方式来访问

import $http from 'http';
// $http.foo()
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题