Nuxt.js: Custom route names

Created on 25 Jul 2018  路  18Comments  路  Source: nuxt/nuxt.js

What problem does this feature solve?

Currently, urls are based on the filename with the pages directory. I'd like to be able to use

export default {
    name: 'custom-page-name',
    ...
};

and have it set that as my route name for that page. That way, I can use <nuxt-link :to="{ name: 'custom-page-name' }">Link</nuxt-link>, without worrying that if I change my page file name (and therefore the URL) it'll break all my links.

What does the proposed changes look like?

export default {
    name: 'custom-page-name', // This is already a Vue convention for naming components
    ...
};

<nuxt-link :to="{ name: 'custom-page-name' }">Link</nuxt-link>

This feature request is available on Nuxt community (#c7439)
feature-request

Most helpful comment

Bumping this one so stalebot doesn't swoop in and ruin our dreams 馃槩

All 18 comments

@SteveEdson the team's current position is to maintain the convention for now.

An alternative is to use the router module or extendRoutes in conjunction with routes.splice(0, routes.length, ...[new routes]).

router module or extendRoutes is not hard to manage with a lot pages.
this should be open as feature if the team is not plan to implant it soon.

@patrickkh7788 If someone wants to submit a PR, we'd be happy to look over it!

So is this now implemented? Or why is it closed?

@simllll As you can read a few comments above yours, it's not planned to get implemented very soon.

Ah sorry, I must have missed that. That's sad, I was running in the exact issue right now and was looking for a solution. Especially because it is not possible to pass object as parameters with a path, only with named route.

Maybe you could use the meta object of the routes 馃

I want to add my support to this for an additional reason: transitioning existing Vue apps. Rather than doing find and replace on all route names in existing pages/components, you can simply set the name and be more confident that all your existing links work. One less barrier to moving to Nuxt.

Keeping it open since this could be really handy when working with pages, but this has to be well done.

Right now, we have no way to read the page file and get the name given in it, and using RegExp is not a good idea since it would lead to more issues.

Can we add the Hacktoberfest label to this issue please? It'd be great for the additional support. Thanks.

@SteveEdson Thanks for the request but as the issues incorporates a lot of internal changes it isn't "suitable" for hacktoberfest 馃槮

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

Ah, I have been looking to switch to nuxt.js lately, is it possible to do nested named routing?

For example:

export default new Router({
    mode: 'history',
    routes: [
        {
            path: '/:locale',
            component: TheRoot,
            props: true,
            children: [
                {
                    path: '/sports/:id',
                    name: 'Sport',
                    component: Sport
                }
            ]
        }
    ]
})

Now I would like to be able to simply access this with :to="{ name: 'Sport', params: { id: 2} }" without having to figure out what the current locale is all the time with the full path :to="`${locale}/sports/2`"

Is this currently possible? Or are we waiting for this issue to be resolved to do this?

Bumping this one so stalebot doesn't swoop in and ruin our dreams 馃槩

@aaronransley Stale bot isn't closing feature request anymore.

nuxt-router-extras may be related 馃槉 Updated version: nuxt-community/router-extras-module

Would be great to be able to define custom names, to keep the code DRY.

I think the custom path name could be a static property of the component.

And then this class can be imported into places where the paths need to be referenced.

The solution for this is to use the router-extra-module.

The documentation will be updated to point this module :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vadimsg picture vadimsg  路  3Comments

nassimbenkirane picture nassimbenkirane  路  3Comments

vadimsg picture vadimsg  路  3Comments

mattdharmon picture mattdharmon  路  3Comments

pehbehbeh picture pehbehbeh  路  3Comments