[Vuejs]-VUE Trailing path queries

0👍

if you want to redirect to another route inside a router guard, you need to call next() with the new params.

See https://router.vuejs.org/guide/advanced/navigation-guards.html#global-guards

Specifically:

Blockquote
next(‘/’) or next({ path: ‘/’ }): redirect to a different location. The current navigation will be aborted and a new one will be started. You can pass any location object to next, which allows you to specify options like replace: true, name: ‘home’ and any option used in router-link’s to prop or router.push

So something like this:

router.beforeEach((to, from, next) => {
  let lang = from.query.lang;
  //Changing language 
  i18n.locale = lang || 'en';

  //This now should redirect
  if(!lang){
    console.log('Trying to push path: '+to.path+'/?lang='+lang)
    next({ path: `${to.path}/?lang=${lang}` });
    return; // this is needed just to make sure code below this get
  }

Hope this helps.

Leave a comment