-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What is the best way to load json. #123
Comments
Is this a file you will be changing often on the fly? If this configuration can be baked into the builds, another option could be to import the JSON file in the components that need it, utilizing Webpack's JSON loader? Or convert it to a JS module and export the values. |
Hi @husayt To answer your questions: What is wrong with the example below?When the http call is made from the server, axios has no idea that you're on http://localhost:3000, so it can't call the route It can be made by setting a // nuxt.config.js
module.exports = {
env: {
baseURL: (process.env.NODE_ENV === 'production' ? 'http//your-url' : 'http://localhost:3000')
}
} Then in you data method: async asyncData({ req, params, store }) {
const authors = await axios.get(process.env.baseURL + '/data.json').then(res => res.data)
return { authors }
})
} I recommend to use the axios module to avoid giving a Is
|
@atinux thanks for very detailed reply. This will be very useful addition to FAQ page. We covered the following data file reading patterns:
There might be even more patterns. So all is cool with running it in dev mode. Now I hit two problems running from static generated files (after
Thanks |
The data embedded in For your error you're experiencing, can you provide me some code samples so I can reproduce this behaviour on my setup? |
Hi @atinux, thanks for detailed reply. As for disappearing data here is the screen gif, showing that data disappears few seconds after loading. Here is the code: <template>
<section class="container">
<h1 class="title"> {{title}}</h1>
<li v-for="author in authors">
<nuxt-link :to="{ name: 'authors-id', params: { id: author.id } }">{{ author.text }}</nuxt-link>
</li>
</section>
</template>
<script>
import {axiosGet} from '~plugins/util'
export default {
data({ env,req,params,store}) {
return axiosGet(env.baseURL +'rest/authors.json')
.then((res) => {
return {
authors: res.data,
title: 'Müəlliflər'
}
})
},
head() {
return {
title: this.title
}
},
}
</script> |
@husayt I've encountered the exact same issue. It appears to be an issue with Vue's vdom throwing a mismatch of the server rendered DOM and the virtual DOM. I've encountered it with homegrown Vue SSR and Nuxt. If the Nuxt team can get a fix, that'd be dope. |
@husayt I'm not sure if this is causing your issue, but you have invalid HTML in your template. Your |
is there a proper way to automatically webpack assets such as images that are defined in a json file? |
hi i need help i'm try trans variable {{location.slug}} in nuxt-link ,
|
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I have a json file with data to initialise my pages. I put it in
/static
folder e.g./static/data.json
. Then I useaxios.get()
to load it from/data.json
. That works on the client, but fails on the server.I have these questions, which I think will be very useful for wider audience as well:
/static
the best place to put data files?Thanks in advance
The text was updated successfully, but these errors were encountered: