一、定义全局方法
代码如下:
//main.js中
import axios from 'utils'
app.config.globalProperties.$axios = axios
组件中使用:
//组件中使用
<script setup>
import { reactive, ref, getCurrentInstance } from "vue";
const _this = getCurrentInstance()
const { $axios} = _this.appContext.config.globalProperties;
//直接调用
$axios.post("/api/echartsData").then((res) => {
//赋值
});
</script>
二、定义全局组件
代码如下:
//main.js中
import secondNav from "./components/secondNav.vue";
const app = createApp(App)
app.component('secondNav ', secondNav)
组件中使用:
//组件中使用
<template>
<second-nav />
</template>
总结
vue2 中 Vue.prototype;vue3中使用app.config.globalProperties
参考https://blog.csdn.net/qq_42092177/article/details/120529958