// 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)}) }); ....... }