Posts.vue 722 B

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