vue3.0 setup 中使用 globalProperties


// 2.0
Vue.prototype.$http = () => {}

// 3.0
const app = Vue.createApp({})
app.config.globalProperties.$http = () => {}

 

最常用的场景,在页面渲染前,通过http请求获取需要显示的数据。

在2.0,通过在vue原型上添加$http 属性,可以在组件实例中通过this.$http使用;

//2.0
 created(){
      this.$http.get('/getData').then().catch(err => {console.log(err)})
}

而在3.0的settup中是没有this的。

import { getCurrentInstance,onBeforeMount} from 'vue';
setup (props,context) {
      const {ctx } = getCurrentInstance();
      onBeforeMount(()=>{
        ctx.$http.get('/getData').then().catch(err => {console.log(err)})
      });
      .......
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM