index.js 993 B

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