Customising URL paths using Navigation Guards

Viewed 23

I have an application with two pages.

Page one:

<form>
    <input pattern="\d*" maxlength="4" v-model="id" required>
    <router-link :to="`/${id}`">
       <div @click="info">GET</div>
    </router-link>
</form>

Using ('0000' + this.id).slice(-4), I am able to transfer 4 digits to 2nd page with desired URL like:

Note: At the moment, I can pass the letters also because I am not validating

My question is how to use the next({path: ''}) Navigation Guards. I want the following to happen:

I tried using beforeEnter, but I think I am missing some things. This is my route.js file.

const routes=[
    {
        name:'Info',
        path:'/:id',
        component: Info,
        beforeEnter: (to, from, next) => {     
            to.params.myCustomizations = {
                id_num: ('0000' + to.params.id).slice(-4)     
            }
            next({path: `/${to.params.myCustomizations}`})
          },
    },
];

I would appreciate any help you can provide.

1 Answers

I think instead of

to.params.myCustomizations = {
    id_num: ('0000' + to.params.id).slice(-4)     
}
next({path: `/${to.params.myCustomizations}`})

You should write:

next({path: `/${('0000' + to.params.id).slice(-4)}`})

Or:

to.params.id = ('0000' + to.params.id).slice(-4)
next()

Ps: Why the padded id? The number would be good by itself.

Related