全局注册axios

main
zggsong 2 years ago
parent 4936574df4
commit 0f7e8a4b10

BIN
.DS_Store vendored

Binary file not shown.

@ -18,20 +18,20 @@
</template> </template>
<script setup> <script setup>
import { ref, reactive } from "vue" import { ref, reactive, getCurrentInstance } from "vue"
import { useRouter, useRoute } from "vue-router" import { useRouter, useRoute } from "vue-router"
import { ElMessage } from 'element-plus' import { ElMessage } from 'element-plus'
import axios from 'axios'
axios.defaults.baseURL = '/api'
const router = useRouter(); const router = useRouter();
const route = useRoute(); // const route = useRoute(); //
let user = reactive({ tel: "", pwd: "" }); let user = reactive({ tel: "", pwd: "" });
// axios
const {appContext} = getCurrentInstance();
function login() { async function login() {
// //
axios.post('/auth/login', null, await appContext.config.globalProperties.$http.post('/auth/login', null,
{ {
params: { params: {
telephone: user.tel, telephone: user.tel,

@ -21,20 +21,19 @@
</template> </template>
<script setup> <script setup>
import { ref, reactive } from 'vue' import { ref, reactive, getCurrentInstance } from 'vue'
import { useRoute, useRouter } from 'vue-router' import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus' import { ElMessage } from 'element-plus'
import axios from 'axios'
axios.defaults.baseURL = '/api'
const router = useRouter() const router = useRouter()
const route = useRoute()// const route = useRoute()//
const {appContext} = getCurrentInstance()
let user = reactive({ name: '', tel: '', pwd: '' }) let user = reactive({ name: '', tel: '', pwd: '' })
function register() { async function register() {
axios.post('/auth/register', null, await appContext.config.globalProperties.$http.post('/auth/register', null,
{ {
params: { params: {
name: user.name, name: user.name,

@ -3,8 +3,9 @@ import { createApp } from "vue";
import App from "./App.vue"; import App from "./App.vue";
import ElementPlus from "element-plus"; import ElementPlus from "element-plus";
import "element-plus/dist/index.css"; import "element-plus/dist/index.css";
import { createRouter, createWebHistory } from "vue-router"; import { createRouter, createWebHistory } from "vue-router";
import axios from "axios";
axios.defaults.baseURL = '/api'
const routes = [ const routes = [
{ path: "/login", component: () => import("./components/login/Index.vue") }, { path: "/login", component: () => import("./components/login/Index.vue") },
@ -19,9 +20,9 @@ const router = createRouter({
history: createWebHistory(), history: createWebHistory(),
routes, routes,
}); });
//https://cn.vuejs.org/api/application.html#app-config-compileroptions
let app = createApp(App); let app = createApp(App);
app.use(router); app.use(router);
app.use(ElementPlus); app.use(ElementPlus);
app.config.globalProperties.$http = axios;
app.mount("#app"); app.mount("#app");

Loading…
Cancel
Save