A
A
Andrej Sharapov2020-04-27 19:15:02
Vue.js
Andrej Sharapov, 2020-04-27 19:15:02

How to get data through Nuxt template?

In the nuxt template (default) I try to write a request to the json file via axios, but the data does not come on the network (f12 - network). Tell me how to fix this?

<v-navigation-drawer v-model="rightDrawer" :right="right" temporary fixed nav>
      <perfect-scrollbar>
        <ProjectBarList :projects="projects" />
      </perfect-scrollbar>
    </v-navigation-drawer>


const URL_PROJECTS = '~/../files/json/projects.json'

export default {
  async asyncData({ $axios }) {
    const getProjectList = async () => {
      try {
        const res = await $axios.$get(URL_PROJECTS)
        return res
      } catch (e) {
      } finally {
      }
    }

    const projects = await getProjectList()

    return {
      projects
    }
  }
}


This works if moved to any page, but I need the data to be displayed in the sidebar and be available on all pages. Is there any way to get the file from there? Share your solution please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Glebov, 2020-04-27
@SkiperX

asyncData only works on page components

data() {
  return {
    projects: []
  }

}
async fetch() {
  this.projects = await this.$axios.$get(URL_PROJECTS)
        }

so you can
either through https://ru.nuxtjs.org/guide/vuex-store/#%D0%B4%D0%...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question