0

I'm building an app using vue and vue-router. I've created a Login component with the usual fields for email and password, and a forgot password router-link. What I wanted to do was to pass the email from the email field if the user has typed it in already but then clicked on the forgot password button. This is how the user gets transferred to the resetPassword component:

<router-link :to="{ name: 'resetPassword',  query: { email } }">
    Forgot password
</router-link>

and this is how I handle the route within the router:

{
  path: '/resetpassword/:email?',
  name: 'resetPassword',
  component: require('../components/ResetPassword').default,
  props: route => ({ email: route.query.email }),
},

this solution works but my only issue is that if the user hasn't typed in an email I will get an url that will look like this:

localhost:8080/resetpassword?email=

while I want it to look like this:

localhost:8080/resetpassword

how can I omit the empty email query parameter? Do I have to do something in the config of vue-router? I couldn't find something about this in the documentation.

1
  • Not seeing much out there for hiding the optional query parameter, but an alternative pattern to consider is to use vuex to hold the email and read it from the store in forgot_password view. Commented Jul 12, 2019 at 12:50

1 Answer 1

0

I've found the solution. The only change that was need was this:

<router-link :to="{ name: 'resetPassword',  query: { email || undefined} }">
    Forgot password
</router-link>

and so when the parameter email hasn't been set it is also not being added to the url.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.