index.js 856 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import Vuex from 'vuex'
  2. const createStore = () => {
  3. return new Vuex.Store({
  4. state: {
  5. headlines: [],
  6. loading: false,
  7. category: ''
  8. },
  9. mutations: {
  10. setHeadLines (state, headlines) {
  11. state.headlines = headlines
  12. },
  13. setLoading (state, loading) {
  14. state.loading = loading
  15. },
  16. setCategory (state, category) {
  17. state.category = category
  18. }
  19. },
  20. actions: {
  21. async loadHeadLines ({ commit }, apiUrl) {
  22. commit('setLoading', true)
  23. const { articles } = await this.$axios.$get(apiUrl)
  24. commit('setLoading', false)
  25. commit('setHeadLines', articles)
  26. }
  27. },
  28. getters: {
  29. headlines: state => state.headlines,
  30. loading: state => state.loading,
  31. category: state => state.category
  32. }
  33. })
  34. }
  35. export default createStore