I have a url : http://local-pr.local?id=12
In page.vue :
- I use :
console.log(this.$route.query.id)
and get it successfully.
And now I want to get that params in the file : page.js
import addon from './addon'
import user from './user'
// I want to check if there is a new params id that has it export default
if (this.$route.query.id) {
export default [...addon, ...user]
}
console.log(this.$route.query.id)
ERROR: Reason: TypeError: Cannot read properties of undefined (reading '$route')
Is there any way to get params in js file? Please give me some experience. Thank you.
CodePudding user response:
You could export a function that takes the $route
as parameter :
import addon from './addon'
import user from './user'
export default function checkParams(route) {
if(this.$route.query.id){
return [...addon, ...user]
}
}
and in any component you could do :
import checkParams from './page.js'
.....
let data=checkParams(this.$route)
CodePudding user response:
this.$route method only works in .vue file.
to access query params in separate js you need to store query params in localStorage and fetch from js.