-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathblog.vue
More file actions
91 lines (88 loc) · 2.1 KB
/
blog.vue
File metadata and controls
91 lines (88 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<template>
<div>
<div class="list-news-wrap">
<ul class="list-news">
<ListPostItem
v-for="post in posts"
:key="post.id"
:post="post"
:random-posts="random_posts"
/>
</ul>
<Pagination :current-page="currentPage" :total-pages="totalPages" />
</div>
</div>
</template>
<script>
import ListPostItem from '~/components/ListPostItem'
import Pagination from '~/components/Pagination.vue'
import { getRandomArrayElement, BASE_API_LINK } from '~/common/utils'
export default {
name: 'Blog',
components: { ListPostItem, Pagination },
key: (to) => to.fullPath,
watchQuery: ['page'],
data() {
return {
posts: [],
random_posts: [],
currentPage: 1,
totalPosts: 0,
totalPages: 0,
title: 'Blog'
}
},
asyncData({ $axios, query, error }) {
const page = +query.page || 1
return $axios
.get(
`${BASE_API_LINK}/wp-json/wp/v2/posts?_embed&page=${page}&per_page=20`
)
.then((res) => {
return {
posts: res.data,
totalPosts: +res.headers['x-wp-total'],
totalPages: +res.headers['x-wp-totalpages'],
currentPage: +page,
random_posts: getRandomArrayElement(res.data, 10),
title:
'Result page ' +
+page +
' of ' +
+res.headers['x-wp-totalpages'] +
' for Blog'
}
})
.catch((e) => {
console.log('request failed', e)
error({ statusCode: 404, message: e })
})
},
transition(to, from) {
if (!from) {
return 'page'
}
return +to.query.page < +from.query.page ? 'slide-right' : 'slide-left'
},
head() {
return {
title: this.title,
meta: [
// hid is used as unique identifier. Do not use `vmid` for it as it will not work
// {
// hid: 'description',
// name: 'description',
// content: 'My custom description'
// }
]
}
}
}
</script>
<style lang="scss">
.list-news {
list-style-type: none;
padding: 0;
margin: 0;
}
</style>