|
2 | 2 | <div v-if="isValidPost()"> |
3 | 3 | <h1 class="title" v-html="post.title.rendered"></h1> |
4 | 4 | <div class="news-content" v-html="post.content.rendered"></div> |
| 5 | + |
| 6 | + <ul class="list-news"> |
| 7 | + <ListPostItem |
| 8 | + v-for="p in random_posts" |
| 9 | + :key="p.id" |
| 10 | + :post="p" |
| 11 | + :random_posts="random_posts" |
| 12 | + /> |
| 13 | + </ul> |
5 | 14 | </div> |
6 | 15 | </template> |
7 | 16 |
|
8 | 17 | <script> |
| 18 | +import ListPostItem from '~/components/ListPostItem' |
| 19 | +
|
9 | 20 | export default { |
10 | 21 | name: 'P', |
| 22 | + components: { ListPostItem }, |
11 | 23 | data() { |
12 | 24 | return { |
13 | | - post: {} |
| 25 | + post: {}, |
| 26 | + random_posts: [] |
14 | 27 | } |
15 | 28 | }, |
16 | 29 | head() { |
@@ -58,27 +71,48 @@ export default { |
58 | 71 | } |
59 | 72 | : {} |
60 | 73 | }, |
61 | | - asyncData({ $axios, params, error }) { |
| 74 | + async asyncData({ $axios, params, error }) { |
62 | 75 | console.log('asyncData post single', params) |
63 | 76 |
|
64 | 77 | if (!params.post) { |
65 | 78 | const slug = params.slug |
66 | | - return $axios |
67 | | - .get(`https://techtalk.vn/wp-json/wp/v2/posts?_embed&slug=${slug}`) |
68 | | - .then((res) => { |
69 | | - if (!res.data.length) |
70 | | - error({ statusCode: 404, message: 'Post not found!' }) |
71 | | - return { |
72 | | - post: res.data.length ? res.data[0] : [] |
73 | | - } |
74 | | - }) |
75 | | - .catch(function(ex) { |
76 | | - console.log('parsing failed', ex) |
77 | | - error({ statusCode: 404, message: ex }) |
78 | | - }) |
| 79 | + try { |
| 80 | + const res = await $axios.get( |
| 81 | + `//techtalk.vn/wp-json/wp/v2/posts?_embed&slug=${slug}` |
| 82 | + ) |
| 83 | + const resRd = await $axios.get( |
| 84 | + `//techtalk.vn/wp-json/wp/v2/posts?_embed&categories=${ |
| 85 | + res.data[0].categories[0] |
| 86 | + }` |
| 87 | + ) |
| 88 | +
|
| 89 | + if (!res.data.length) |
| 90 | + error({ statusCode: 404, message: 'Post not found!' }) |
| 91 | + return { |
| 92 | + post: res.data.length ? res.data[0] : [], |
| 93 | + random_posts: resRd.data.length ? resRd.data : [] |
| 94 | + } |
| 95 | + } catch (ex) { |
| 96 | + console.log('parsing failed', ex) |
| 97 | + error({ statusCode: 404, message: ex }) |
| 98 | + } |
| 99 | + // return $axios |
| 100 | + // .get(`//techtalk.vn/wp-json/wp/v2/posts?_embed&slug=${slug}`) |
| 101 | + // .then((res) => { |
| 102 | + // if (!res.data.length) |
| 103 | + // error({ statusCode: 404, message: 'Post not found!' }) |
| 104 | + // return { |
| 105 | + // post: res.data.length ? res.data[0] : [] |
| 106 | + // } |
| 107 | + // }) |
| 108 | + // .catch(function(ex) { |
| 109 | + // console.log('parsing failed', ex) |
| 110 | + // error({ statusCode: 404, message: ex }) |
| 111 | + // }) |
79 | 112 | } else { |
80 | 113 | return { |
81 | | - post: params.post |
| 114 | + post: params.post, |
| 115 | + random_posts: params.random_posts |
82 | 116 | } |
83 | 117 | } |
84 | 118 | }, |
|
0 commit comments