Skip to content

Commit

Permalink
组合式api的全局挂载与使用,对接口
Browse files Browse the repository at this point in the history
  • Loading branch information
Yutta82 committed May 11, 2023
1 parent 571e51e commit 04d8a84
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/components/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<Methods></Methods>
<Separator></Separator>
<ModularApi></ModularApi>
<Separator></Separator>
<Send></Send>
</template>

<script>
Expand All @@ -17,6 +19,7 @@
import Data from './Data.vue';
import Methods from './Methods.vue';
import ModularApi from './ModularApi.vue';
import Send from './SendRequest.vue';
// 组件就是一个普通的js对象
export default{
Expand All @@ -31,6 +34,7 @@
Data,
Methods,
ModularApi,
Send,
},
};
</script>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Data.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default {
getData() {
let id =Math.random()*10;
//2.使用axios 进行post请求
this.$request.post("TestController/testParams", {
this.$request.post("/TestController/testParams", {
id
}).then((res) => {
//请求成功的回调函数
Expand Down
2 changes: 1 addition & 1 deletion src/components/Methods.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default {
// console.log('globalToken :>> ', this.$global.jwtToken);
// // 成功请求接口并拿到数据 :D
// this.$request.post("Reserve/HistoryRecord", {})
// this.$request.post("/Reserve/HistoryRecord", {})
// .then((res)=>{
// console.log(res);
// this.historyArray = res.data;
Expand Down
2 changes: 1 addition & 1 deletion src/components/ModularApi.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<h1>组合式Api</h1>
<h3>组合式Api</h3>
<div>{{ s }}</div>
<div>{{ person}}</div>
<button @click="setAge">改变年龄</button>
Expand Down
35 changes: 35 additions & 0 deletions src/components/SendRequest.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<template>
<h3>请求展示</h3>
<button @click="AvailableRoom">查看可用琴房</button>
<br>
<button @click="CreateReserve">预约</button>
</template>

<script setup>
import { inject, onMounted } from "vue";
const request = inject('$request');
function AvailableRoom() {
request.post('/ReserveController/availableRoom', {
StartTime: '2021-10-01 08:00:00',
EndTime: '2021-10-01 09:00:00',
}).then((res) => {
console.log(res);
}).catch((err) => {
console.log(err);
});
}
function CreateReserve() {
request.post('/ReserveController/createReserve', {
StartTime:'2021-10-01 08:00:00',
EndTime:'2021-10-01 09:00:00',
StudentId:1,
RoomId:1,
}).then((res)=>{
console.log(res);
}).catch((err)=>{
console.log(err);
})
}
</script>
4 changes: 2 additions & 2 deletions src/config/axiosConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import axios from 'axios'
import global from '../common/global.js';

// java项目路径
// axios.defaults.baseURL = 'http://localhost:8080/api/'
axios.defaults.baseURL = 'http://localhost:8080/api'
// c#项目路径
axios.defaults.baseURL = 'https://localhost:7082/api/'
// axios.defaults.baseURL = 'https://localhost:7082/api'
// 携带 cookie,对目前的项目没有什么作用,因为我们是 token 鉴权
axios.defaults.withCredentials = false
// 请求头,headers 信息
Expand Down
4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import global from './common/global.js'
const app = createApp(App);
// 将 axios 挂载为 app 的全局自定义属性
// 每个组件可以通过 this 直接访问到全局挂载的自定义属性
app.config.globalProperties.$request = axios;
app.config.globalProperties.$global = global;
app.provide('$request',axios);
app.provide('$global',global);
window.vm = app.mount('#app');


Expand Down

0 comments on commit 04d8a84

Please sign in to comment.