-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy pathPost.vue
75 lines (72 loc) · 1.31 KB
/
Post.vue
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
<template>
<div class="post">
<div class="loading" v-if="loading">Loading...</div>
<div v-if="error" class="error">
{{ error }}
</div>
<transition name="slide">
<!--
giving the post container a unique key triggers transitions
when the post id changes.
-->
<div v-if="post" class="content" :key="post.id">
<h2>{{ post.title }}</h2>
<p>{{ post.body }}</p>
</div>
</transition>
</div>
</template>
<script>
import { getPost } from './api'
export default {
data () {
return {
loading: false,
post: null,
error: null
}
},
created () {
this.fetchData()
},
watch: {
'$route': 'fetchData'
},
methods: {
fetchData () {
this.error = this.post = null
this.loading = true
getPost(this.$route.params.id, (err, post) => {
this.loading = false
if (err) {
this.error = err.toString()
} else {
this.post = post
}
})
}
}
}
</script>
<style>
.loading {
position: absolute;
top: 10px;
right: 10px;
}
.error {
color: red;
}
.content {
transition: all .35s ease;
position: absolute;
}
.slide-enter {
opacity: 0;
transform: translate(30px, 0);
}
.slide-leave-active {
opacity: 0;
transform: translate(-30px, 0);
}
</style>