Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
18
rated 0 times [  22] [ 4]  / answers: 1 / hits: 5582  / 4 Years ago, wed, july 8, 2020, 12:00:00

The vue-router is functioning fine but we would like to push a route in another file. Some code to clarify:


// src/router/index.ts
import { route } from 'quasar/wrappers'
import VueRouter from 'vue-router'
import routes from './routes'

export default route(function ({ Vue }) {
Vue.use(VueRouter)
const Router = new VueRouter({
scrollBehavior: () => ({ x: 0, y: 0 }),
routes,
mode: process.env.VUE_ROUTER_MODE,
base: process.env.VUE_ROUTER_BASE,
})

return Router
})

It would be great to be able to adjust the route in another file like this:


// src/services/auth/authService.ts
import router from 'src/router'

if (router.currentRoute.path === '/login') {
console.log('authService push to /');
router.push('/')
}

But this throws the error:



TS2339: Property 'currentRoute' does not exist on type 'RouteCallback'.



We're probably not exporting/importing the router correctly.


More From » typescript

 Answers
4

Fixed it by exporting the Router correctly:


import { route } from 'quasar/wrappers'
import VueRouter from 'vue-router'
import routes from './routes'

export const Router = new VueRouter({
scrollBehavior: () => ({ x: 0, y: 0 }),
routes,
mode: process.env.VUE_ROUTER_MODE,
base: process.env.VUE_ROUTER_BASE,
})

export default route(function ({ Vue }) {
Vue.use(VueRouter)
return Router
})

And then consuming it outside of Vue where there is no this context like this:


import { Router } from 'src/router'

if (Router.currentRoute.path === '/login') {
console.log('authService push to /')
Router.push('/')
}

Hope this might help others running into the same issue.


[#3251] Sunday, July 5, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cadendericki

Total Points: 482
Total Questions: 109
Total Answers: 103

Location: Ecuador
Member since Thu, Jun 4, 2020
4 Years ago
;