added posts
This commit is contained in:
@@ -2,29 +2,49 @@
|
|||||||
import { ref, onMounted } from "vue";
|
import { ref, onMounted } from "vue";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
|
||||||
const posts = ref([]);
|
const post = ref(null);
|
||||||
|
const fetched = ref(false);
|
||||||
|
let posts = [];
|
||||||
|
let idx = 0;
|
||||||
|
let len = 0;
|
||||||
|
|
||||||
async function fetchPosts() {
|
async function fetchPosts() {
|
||||||
try {
|
try {
|
||||||
const res = await axios.get("https://adam-french.co.uk/api/posts");
|
const res = await axios.get("https://adam-french.co.uk/api/posts");
|
||||||
posts.value = res.data;
|
posts.value = res.data;
|
||||||
|
fetched.value = true;
|
||||||
|
post.value = posts[0];
|
||||||
|
len = posts.length;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function nextPost() {
|
||||||
|
post.value = posts[idx];
|
||||||
|
idx = (idx + 1) % len;
|
||||||
|
}
|
||||||
|
|
||||||
|
function prevPost() {
|
||||||
|
post.value = posts[idx];
|
||||||
|
idx = (idx - 1) % len;
|
||||||
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
fetchPosts();
|
fetchPosts();
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div v-for="post in posts">
|
<div>
|
||||||
<h2>{{ post.title }}</h2>
|
<h2>{{ post.title }}</h2>
|
||||||
<p>By: {{ post.author.username }}</p>
|
<p>By: {{ post.author.username }}</p>
|
||||||
<div>{{ post.content }}</div>
|
<div>{{ post.content }}</div>
|
||||||
<small
|
<small
|
||||||
>Created at: {{ new Date(post.CreatedAt).toLocaleString() }}</small
|
>Created at: {{ new Date(post.CreatedAt).toLocaleString() }}</small
|
||||||
>
|
>
|
||||||
|
<button @click="nextPost">Next</button>
|
||||||
|
<button @click="prevPost">Prev</button>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user