Dawid Zbiński
Dawid Zbiński

Reputation: 5826

Accessing route object from VueRouter instance

I'm working on a small service which will work with current route's query parameters.

Unfortunately, it seems like VueRouter has no route parameter, so I'm unable to access information I need in service.

import router from "../router";

class QueryParametersService {
    constructor() {
        this.router = router;
    }

    getAll() {
        // Unfortunately, `route` does not exist there
        // but for you, to get the idea, I want to achieve something like this
        return this.router.route.query;
    }
}

Of course, router is properly instantiated and works well. Is there any way to access current route from within VueRouter instance? Or is there any way to access the current route from external service? Thank you in advance.

Upvotes: 1

Views: 1602

Answers (2)

Dawid Zbiński
Dawid Zbiński

Reputation: 5826

My bad, I haven't read the whole documentation of VueRouter's API.

Current route is stored in router.currentRoute.

Unfortunately, it's probably a magic method, that's why it not visible when you console.log the instance.

Upvotes: 4

jordiburgos
jordiburgos

Reputation: 6302

Use this.$route.params.varName

this.$route would be available if vue-router is loaded.

Upvotes: 1

Related Questions