如何将对象安装在vue2.7实例上?

发布于 2025-02-12 14:23:29 字数 384 浏览 0 评论 0原文

我刚刚将项目完全升级到VUE2.7,该项目对VUE3功能提供了支持。

就像我尝试的是,在vue实例链中获取对象,例如 从“ vue”导入vue; vue.protype。$ http = http;,但没有任何方法或挂钩,例如createApp vue2.7中支持的,所以我如何访问它($ http )在我的组件中?

我暂时只能做console.log(getCurrentInstance())

最后,通过getCurrentInstance()。代理。__proto __.__ proto __。$ http,例如不愉快的代码,是否有其他更好的快捷方式?谢谢。

I have just completely upgraded my project to Vue2.7, which a backported support for Vue3 feature.

Like what I try is, get the object the in vue instance chain, such as
import Vue from 'vue'; Vue.prototype.$http = http; but no any method or hook such as createApp supported in vue2.7, so how to I access it(the $http) in my component?

only what I can do for now just console.log(getCurrentInstance()).

And finally get it by getCurrentInstance().proxy.__proto__.__proto__.$http, such unpleasant code, is it any other better shortcut to do this? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み格子的夏天 2025-02-19 14:23:29

我建议使用提供/注入使所有组件中的Axios可用。

https://vuejs.org/guide/components/components/provide-inject.html

I would suggest using provide/inject to make the axios available in all your components.

https://vuejs.org/guide/components/provide-inject.html

月光色 2025-02-19 14:23:29

通过热心的人jacek来找出路,只需使用提供和注入来解决它。

一样

const vue = new Vue({
  setup() {
    provide('http', http)
  }, ...
}

Find out the way by warmed-heart people Jacek, just use provide and inject to solve it.

Just like

const vue = new Vue({
  setup() {
    provide('http', http)
  }, ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文