Nuxt.js: 如何启动具有特定路径值的服务器?
Nuxt.js: how to launch the server with a specific path value?
在我的 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
”
不知道您是否还在寻找答案,但是,您是否考虑过设置一个中间件文件来重定向用户?我将一个用于身份验证,因此如果用户未登录中间件,则在请求“/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']
},
并且所有请求都应重定向到“/1”。
在我的 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
”
不知道您是否还在寻找答案,但是,您是否考虑过设置一个中间件文件来重定向用户?我将一个用于身份验证,因此如果用户未登录中间件,则在请求“/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']
},
并且所有请求都应重定向到“/1”。