1
2
3
4
app.config.globalProperties.$axios = $axios

const { proxy } = getCurrentInstance() as any
proxy.$axios.get()

事实上官方是不建议应用使用 getCurrentInstance 方法的

1
2
3
4
5
6
7
8
9
10
11
// main.ts
const app = createApp(App)

// 配置全局变量 页面中使用 inject 接收
app.provide('global',{
store,
axios
})

app.mount('#app')

将多个变量混同时注入的目的是为了减小依赖注册及接受的工作量, 在需要接受的页面使用 inject 接受:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 组件中
<script lang="ts" setup>
import { inject } from 'vue';

// 获取全局对象`
const global:any = inject('global')

/**目前标准异步写法 */
global.axios('/harmony/getType').then((result:any) => {
if(result.success){
list.value = result.data
}
}).catch((err:any) => {
console.log(err);
});

</script>