Vue 3 + Vue Router 4 + Pinia store (or any other place outside of vue components)
@KitKit up there gave an example how to get route if you are using Vue 3 and Vue Router 4 in setup
hook. However, what about state management in Pinia store ?
In vue@2 and vue-router@3.5.1: We could have used router.currentRoute.query.returnUrl
like so (example in vuex state management):
import router from "@/router";
const state = initialState;
const getters = {};
const actions = { // your actions };
const mutations = {
loginSuccess(state, user) {
let returnUrl = "";
if(router.currentRoute.query.returnUrl != undefined)
returnUrl = router.currentRoute.query.returnUrl;
},
};
export default {
state,
getters,
actions,
mutations,
};
export const authentication = {
actions: {},
mutations: {},
};
In vue@3 and vue-router@4: We have to append value
to currentRoute
like so:
import router from '@/router';
export const authenticationStore = defineStore('authUser', {
state: (): State => ({
// your state
}),
getters: {
// your getters
},
actions: {
loginSuccess(user: object) {
let returnUrl = '';
if (router.currentRoute.value.query.returnUrl != undefined)
returnUrl = router.currentRoute.value.query.returnUrl;
},
},
});