代码之家  ›  专栏  ›  技术社区  ›  Billal Begueradj

Nuxt.js:如何使用特定的路径值启动服务器?

  •  1
  • Billal Begueradj  · 技术社区  · 6 年前

    在我的Nuxt.js应用程序中,我有一系列嵌套路由。

    .
    ├── 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~
    

    我想做的是当我启动服务器时( yarn run dev http://localhost:3000/1 而不是 http://localhost:3000/

    注意 在这种情况下,一个对应于路径“ /:choice

    1 回复  |  直到 6 年前
        1
  •  3
  •   Andrew1325    6 年前

    不知道您是否还在寻找答案,但是,您是否考虑过设置一个中间件文件来重定向用户?我使用一个用于auth,因此如果用户没有登录中间件,则会在请求“/admin”时重定向到“/login”。除了设置重定向“/”的所有请求外,您可以执行相同的操作。

    要设置它,只需在middleware文件夹中创建一个文件,我们将其命名为redirect.js,并在其中包含如下内容:

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

    然后需要在nuxt.config.js中读取该文件:

    router: {
    
        middleware: ['redirect']
    
      },