Client-side data
Client-side data is data added after page load. This can be data coming from other internal pages, a REST API or a GraphQL API. It's important to only load your data in the mounted
hook to prevent it from being included in the generated markup.
Fetch from internal pages
Fetch page-query
results and page context from other internal pages. The following example fetches data from /other-page
and stores the results.
export default { data () { return { otherPage: null } }, async mounted () { try { const results = await this.$fetch('/other-page') this.otherPage = results.data } catch (error) { console.log(error) } } }
The fetch
method can also be imported from gridsome
.
import { fetch } from 'gridsome' export default { async mounted () { const results = await fetch('/other-page') console.log(results.data) } }
Read more about the $fetch() method.
Fetch from REST API
....Contributions are welcome!
Fetch from GraphQL API
....Contributions are welcome!
Fetch from local YAML files
The following example fetches local YAML files within .vue templates:
- Create a YAML file in
/src/data
folder. For example:products.yaml
- Add
import products from @/data/products.yaml
beforeexport default
function. - Add the data from the YAML file to the data layer by creating a new object key
products
and defining it with the just importedproducts
. Since the object key and the value are the same, we can destructure to justproducts
.
<template> <ul v-for="product in products"> <li v-html="product.title"/> </ul> </template> <script> import products from '@/data/products.yaml' export default { data() { return { products } } } </script>
Fetch from local JSON files
The following example fetches local JSON data within .vue templates:
- Create a JSON file in
/src/data
folder. For example:users.json
- Add
import products from @/data/users.json
beforeexport default
function. - Add the data from the JSON file to the data layer by creating a new object key
users
and defining it with the just importedusers
. Since the object key and the value are the same, we can destructure to justusers
.
<template> <ul v-for="user in users"> <li v-html="user.name"/> </ul> </template> <script> import users from '@/data/users.json' export default { data() { return { users } } } </script>