menu

Questions & Answers

Vue Cannot read properties of undefined (reading '$router')

I am running into an error of accessing this.$route. Every time I try I use it I get the error Uncaught TypeError: Cannot read properties of undefined (reading '$router') I have tried having a const self = this; at the top and trying to use self but that doesn't work. I also tried accessing it after 1 second as I may be trying to access it before Vue is initialized but that also doesn't work. Doesn't anyone have any ideas? Thanks

Comments:
2023-01-24 23:20:03
can you show the code your trying, resorting to const self = this; is bad practice, use arrow functions or .bind(this) to the function
2023-01-24 23:20:03
for now im only trying to console.log(this.$router) in the script tag without any function and im still getting the error
2023-01-24 23:20:03
Did you mean to say, this.$router? Also, it will be better to add the block of code you are trying to address.
2023-01-24 23:20:03
yes i do, its not encapsulated in a function and its at the top level so i dont know why it wont work
2023-01-24 23:20:03
can you show the code you're trying, so your doing <script>console.log(this.$router)</script>?
2023-01-24 23:20:03
are you using vue2 or vue3?
2023-01-24 23:20:03
It totally depends on where this code is located, which isn't shown. Please, provide stackoverflow.com/help/mcve for your problem, see stackoverflow.com/help/how-to-ask
Answers(0) :