Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace option is missing when use beforeEach hook #1090

Closed
viclm opened this issue Jan 11, 2017 · 13 comments
Closed

replace option is missing when use beforeEach hook #1090

viclm opened this issue Jan 11, 2017 · 13 comments

Comments

@viclm
Copy link

viclm commented Jan 11, 2017

The route object doesnot contain any replace imformation, so if i call router.replace() or enable replace in <view-link replace>, then use next(path) always invoke history.push

@fnlctrl
Copy link
Member

fnlctrl commented Jan 11, 2017

It's added in 2.1.0 https://github.com/vuejs/vue-router/releases/tag/v2.1.0

router.beforeEach((to, from, next) => {
  if (to.path === '/foo') {
    next({
      path: '/bar',
      replace: true
    })
  }
})

@fnlctrl fnlctrl closed this as completed Jan 11, 2017
@viclm
Copy link
Author

viclm commented Jan 11, 2017

You are not get it, I do not know the navigation which invoke beforeEach hook is push or replace

@fnlctrl
Copy link
Member

fnlctrl commented Jan 11, 2017

Yes, there's currently no information about whether beforeEach was invoked by push/replace.
Can you explain why you need it?

@fnlctrl fnlctrl reopened this Jan 11, 2017
@viclm
Copy link
Author

viclm commented Jan 11, 2017

I need every route contain the same query info, but do not want to copy it in every navigation, in beforeEach hook, just add it if not provided, so i need the next(path) in the hook keep the same behavior

@fnlctrl
Copy link
Member

fnlctrl commented Jan 11, 2017

In this case where you want to enforce query params, simply call next again with replace: true

router.beforeEach((to, from, next) => {
    next()
    next({
        query: {
            foo: 'bar'
        },
        replace: true
    });
});

@fnlctrl fnlctrl closed this as completed Jan 11, 2017
@BoltDoggy
Copy link

I need the same help. Please open this.

@BoltDoggy
Copy link

#1620

@wanyaxing
Copy link

wanyaxing commented Nov 26, 2017

+1 for this feature.

I want to hook the router in each page in ddtalk app, to add parameter dd_nav_bgcolor, but I found all the links in next are set as router.push...

router.beforeEach ((to, from, next)=> {
    if (to && to.query && !to.query.dd_nav_bgcolor)
    {
        to.query.dd_nav_bgcolor = 'FF08B2F2';
        next(to);
    }
    else
    {
        next();
    }
  });

@carolirod
Copy link

+1

1 similar comment
@sl-nancy
Copy link

+1

@SSSStone
Copy link

SSSStone commented Nov 5, 2018

The same question!

router.beforeEach((to, from, next) => {
  if (to.query.r) {
    next()
  } else {
    next({
      // replace: true,
      path: to.path,
      query: Object.assign(to.query, { r: Math.random() })
    });
  }
})

@mathk
Copy link

mathk commented Mar 13, 2019

+1

@vuejs vuejs deleted a comment from harvey-woo Mar 13, 2019
@posva
Copy link
Member

posva commented Mar 13, 2019

the query is not modifiable, create a new object:

query: Object.assign({}, to.query, { r: Math.random() })
// or
query: { ...to.query, r: Math.random() }

Closing to prevent noise from questions

@vuejs vuejs locked as resolved and limited conversation to collaborators Mar 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants