例:全局使用request网络请求工具
1、引入文件,main.js中添加文件
import { createSSRApp } from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import request from "./router/request"; // 引入封装的请求方法
2、创建app,并执行,main.js添加request
export function createApp() {
const app = createSSRApp(App);
app.use(store).use(router).mount("#app");
app.config.globalProperties.$request = request;//设置全局
return {
app,
};
}
createApp();
3、vue文件中使用
import { getCurrentInstance } from "vue";
const { proxy } = getCurrentInstance();
使用时调用proxy.$request即可