Posts.vue 961 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <template>
  2. <section v-if="opened != null">
  3. <h2>{{ opened.title }}</h2>
  4. <p>{{ opened.body }}</p>
  5. <a
  6. href="#"
  7. @click="closePost()"
  8. @keydown.enter="closePost()"
  9. >
  10. close
  11. </a>
  12. </section>
  13. <ul v-else>
  14. <li
  15. v-for="post in posts"
  16. :key="post.id"
  17. >
  18. <h2>
  19. <a
  20. :href="'#post-'+post.id"
  21. @click="openPost(post)"
  22. @keydown.enter="openPost(post)"
  23. >
  24. {{ post.title }}
  25. </a>
  26. </h2>
  27. </li>
  28. </ul>
  29. </template>
  30. <script>
  31. import { mapState, mapActions } from 'vuex'
  32. export default {
  33. computed: mapState({
  34. opened: state => state.posts.opened,
  35. posts: state => state.posts.all
  36. }),
  37. created () {
  38. this.$store.dispatch('posts/getAllPosts')
  39. },
  40. methods: mapActions('posts', [
  41. 'openPost',
  42. 'closePost'
  43. ]),
  44. metaInfo () {
  45. return {
  46. title: this.opened !== null ? this.opened.title : 'home'
  47. }
  48. }
  49. }
  50. </script>