ServerData.vue
methods: {
async api(url, method, data) {
//url, 호출하는 방식, 서버로 던질 데이터
return await axios({
method: method,
url: url,
data: data,
}).catch((e) => {
console.log(e);
}).data;
},
},
methods: {
async getProductList() {
this.productList = await this.api(
"https://cfa2c3ca-22ae-413f-ba6f-df81768325db.mock.pstmn.io/productList",
"get",
{}
);
console.log(this.productList);
},
async api(url, method, data) {
//url, 호출하는 방식, 서버로 던질 데이터
return await axios({
method: method,
url: url,
data: data,
}).catch((e) => {
console.log(e);
}).data;
},
},
MixinTest.vue
<template>
<div>
<button type="button" @click="getProductList">조회</button>
<table>
<thead>
<tr>
<th>제품명</th>
<th>가격</th>
<th>배송료</th>
<th>카테고리</th>
</tr>
</thead>
<tbody>
<tr :key="i" v-for="(product, i) in productList">
<td>{{ product.product_name }}</td>
<td>{{ product.price }}</td>
<td>{{ product.delivery_price }}</td>
<td>{{ product.category }}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
import ApiMixin from "../api.js";
export default {
name: "",
mixins: [ApiMixin],
components: {},
data() {
return {
productList: [],
};
},
setup() {},
created() {},
mounted() {},
unmounted() {},
methods: {
async getProductList() {
this.productList = await this.$callAPI(
"https://cfa2c3ca-22ae-413f-ba6f-df81768325db.mock.pstmn.io/productList",
"get",
{}
);
console.log(this.productList);
},
},
};
</script>
monitoring.js
export default {
mounted() {
//데이터베이스 시간 저장
},
unmounted() {
//데이터베이스 시간 저장
},
};
라이프사이클을 통해 사용자의 사이트 이용시간을 알고 싶은 경우 mounted
, unmounted
를 사용하여 알 수 있다.
mixin.js
import axios from "axios";
export default {
methods: {
async $api(url, method, data) {
//url, 호출하는 방식, 서버로 던질 데이터
return await axios({
method: method,
url: url,
data: data,
}).catch((e) => {
console.log(e);
}).data;
},
},
};
main.js
import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
import mixins from "./mixins.js";
const app = createApp(App);
app.mixin(mixins);
createApp(App).use(router).mount("#app");
믹스인은 global로도 만들어서 사용할 수 도 있다.