vue.js - Access router instance from my service -
i create auth service (src/services/auth.js
), functions , properties ..
export default { login() { ... } ... }
inside login
function, need redirect user
router.go(redirect)
how can retrieve router instance?
context
in src/main.js
file, create router ..
import vuerouter 'vue-router' vue.use(vuerouter) import route './routes' const router = new vuerouter({ history: false, linkactiveclass: 'active' }) route(router) const app = vue.extend(require('./app.vue'))
in src/routers.js
map routes
export default function configrouter (router) { router.map({ .. }) }
you should export router
instance , import auth.js
service.
here workaround improvements:
src/routes.js
export default { '/': { component: {...} }, '/about': { component: {...} }, ... }
src/router.js
import vue 'vue' import vuerouter 'vue-router' import routes './routes' vue.use(vuerouter) const router = new vuerouter({...}) router.map(routes) // export router instance export default router
src/main.js
import router './router' import app './app' router.start(app, '#app')
src/services/auth.js
import router '../router' export default { login () { // redirect router.go('path') } }
Comments
Post a Comment