Vue-Router: Cannot read property '$route' of undefined - VueJS
15,999
Solution 1
if you want to load data from server (from browser) in mounted lifecycle try this:
export default {
data() {
return {
data: {}
}
},
mounted() {
this.asyncData();
},
methods: {
async asyncData ({ route }) {
let { data} = await axios.get('http://localhost:8000/api/v1/users/' + this.$route.params.id + '/')
this.data = data
}
}
}
Response from server will be available in response
variable.
For SSR
you can do :
async asyncData ({ store, route }) {
let { data} = await axios.get('localhost:8000/api/v1/users/'; + route.params.id + '/')
return {
posts: data
}
}
asyncData
will be called before the components are instantiated, and it doesn't have access to this
. (see https://ssr.vuejs.org/en/data.html Logic Collocation with Components for details)
Solution 2
For SSR you can change
<script>
async asyncData ({ store, route }) {
let { data} = await axios.get('localhost:8000/api/v1/users/' + this.$route.params.id + '/')
return {
posts: data
}
}
</script>
to
<script>
async asyncData ({ route }) {
let { data} = await axios.get('localhost:8000/api/v1/users/' + route.params.id + '/')
return {
posts: data
}
}
</script>
According to the nuxt tutorial you can not have access to this inside asyncData because it is called before initiating the component. Nuxt Documentation
Related videos on Youtube
Author by
KitKit
Updated on June 04, 2022Comments
-
KitKit almost 2 years
Please review my code.
<template> <div class="row flex"> <div class="col-md-6 home_feed"> <post-detail :posts="posts"></post-detail> </div> </div> </template> <script> import axios from 'axios' export default { async asyncData (params) { let { data } = await axios.get('http://localhost:8000/api/v1/users/' + this.$route.params.id + '/') return { posts: data } }, components: { 'post-detail': Item } } </script>
I got this error:
Cannot read property '$route' of undefined
when I asyncdata from params.id, but when I type: console.log(this.$route.params.id), it goes right. How can I fix this-
samayo over 6 yearsCan you do
console.log(this)
inside your export? I'm pretty sure you will need vue instance to access the route -
craig_h over 6 yearsI'm guessing you can't access
this
insideasyncData
. By the looks of thenuxt
documentation (I've never used it) you need to pass in a context to get a route param:asyncData({params, context})
then you can access it as:context.params.id
. -
KitKit over 6 years@craig_h Can I write API in an other ways instead of asyncData to get data?
-
-
KitKit over 6 yearsResponse return undefine, bro!!
-
Mikhail over 6 yearsin this case? in this code
asyncData
is placed inmethods
and must have this.$route -
Mikhail over 6 yearsoh sorry, i changed
data
toresponse
. I correct it back. try this ;) -
KitKit over 6 yearsIt works perfectly. Vote for your answer. Thank you very much. @Mikhail :)
-
KitKit over 6 yearsBut is there a shorter way to do this?
-
Mikhail over 6 yearsActually i was wrong, probably you are using ssr, and you can pass route as a part of an argument to
asyncData
. I editited answer for this case. -
KitKit over 6 yearsPlease check my answer next. Thank you!
-
Mikhail over 6 yearstry to change
this.$route
toroute
insideasyncData
and tell me what happened please -
KitKit over 6 yearsvue.runtime.esm.js:472 [Vue warn]: The client-side rendered virtual DOM tree is not matching server-rendered content. This is likely caused by incorrect HTML markup, for example nesting block-level elements inside <p>, or missing <tbody>. Bailing hydration and performing full client-side render.
-
Mikhail over 6 yearsso, it's warning about ssr problem, not about
route
. Now does axios work correctly (data
contains real data from server)? Do you have this project on github? can you give a link on its, for search ssr problem? -
KitKit over 6 yearsFixed: async asyncData ({ route }) { let { data } = await axios.get('localhost:8000/api/v1/users' + route.params.username + '/') return { users: data } },