2020年10月30日星期五

vue3.0 setup 中使用 globalProperties

// 2.0Vue.prototype.$http = () => {}// 3.0const 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)})  });  .......}

 

原文转载:http://www.shaoqun.com/a/485777.html

马莎:https://www.ikjzd.com/w/2385

mile:https://www.ikjzd.com/w/1746

焦点科技:https://www.ikjzd.com/w/1831


//2.0Vue.prototype.$http=()=>{}//3.0constapp=Vue.createApp({})app.config.globalProperties.$http=()=>{}最常用的场景,在页面渲染前,通过http请求获取需要显示的数据。在2.0,通过在vue原型上添加$http属性,可以在组件实例中通过this.$http使用;//2.0created(
trax:https://www.ikjzd.com/w/1489
巴士物流:https://www.ikjzd.com/w/2329
探险热已对三大无人区环境造成了威胁:http://tour.shaoqun.com/a/72988.html
曝光!小心不良服务商打着亚马逊官方资源来行骗!:https://www.ikjzd.com/home/100824
英西老虎谷暗河漂流门票多少钱?老虎谷暗河漂流攻略?:http://tour.shaoqun.com/a/18885.html

没有评论:

发表评论