简体   繁体   中英

Nuxt.js: how to launch the server with a specific path value?

In my Nuxt.js application, I have a series of nested routes.

.
├── index
│   ├── _choice
│   │   ├── city
│   │   │   ├── index.vue
│   │   │   ├── _zipCode
│   │   │   │   ├── index.vue
│   │   │   │   ├── street
│   │   │   │   │   ├── index.vue
│   │   │   │   │   └── _street.vue
│   │   │   │   └── street.vue
│   │   │   └── _zipCode.vue
│   │   ├── city.vue
│   │   ├── city.vue~
│   │   └── index.vue
│   ├── _choice.vue
│   └── index.vue
├── index.vue
└── index.vue~

What I want to do is that when I launch the server ( yarn run dev ), I want it to point directly into http://localhost:3000/1 instead of http://localhost:3000/ . How to achieve this?

Note that in this case, one corresponds to the path " /:choice "

Don't know if you're still looking for an answer but, have you considered setting up a middleware file to redirect the user? I use one for auth so if user isn't logged in the middleware redirects to "/login" if "/admin" is requested. You could do the same except set up to redirect all request for "/".

To set it up just create a file in the middleware folder, let's call it redirect.js and have something like this in it:

export default function ({store, redirect, route}) {
    const urlRequiresRedirect = /^\/(\/|$)/.test(route.fullPath)
    if (urlRequiresRedirect) {
        return redirect('/1')
    }
    return Promise.resolve
}

then you need that file being read so in nuxt.config.js:

router: {

    middleware: ['redirect']

  },

and all requests should redirect to "/1".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM