Test getFruits action with fruit-api

This commit is contained in:
2020-12-04 02:26:50 +01:00
parent 4b1ac294bd
commit a027285357
12 changed files with 75 additions and 97 deletions

View File

@@ -2,10 +2,8 @@ import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import axios from "axios";
Vue.config.productionTip = false;
Vue.prototype.$http = axios;
new Vue({
router,

8
src/store/actions.js Normal file
View File

@@ -0,0 +1,8 @@
import axios from "axios";
export default {
getFruits: async ({ commit }) => {
const response = await axios.get("http://localhost:3000/fruit");
commit("setFruits", response.data.data);
}
};

5
src/store/getters.js Normal file
View File

@@ -0,0 +1,5 @@
export default {
parsedFruits: () => {
return [{ id: 1 }];
}
};

View File

@@ -1,6 +1,8 @@
import Vue from "vue";
import Vuex from "vuex";
import mutations from "./mutations";
import actions from "./actions";
import getters from "./getters";
export const state = {
fruits: [],
@@ -11,5 +13,7 @@ Vue.use(Vuex);
export default new Vuex.Store({
state,
mutations
mutations,
actions,
getters
});

View File

@@ -1,26 +1,20 @@
<template>
<div>
<h1>Fruits Directory</h1>
{{ fruits }}
{{ this.fruits }}
</div>
</template>
<script>
import { mapState } from "vuex";
export default {
name: "Fruits",
data() {
return {
fruits: []
};
computed: {
...mapState(["fruits"])
},
async mounted() {
await this.getFruits();
},
methods: {
async getFruits() {
const response = await this.$http.get("http://localhost:3000/fruit");
this.fruits = response.data.data;
}
async created() {
await this.$store.dispatch("getFruits");
}
};
</script>