useRequestHeader
使用 useRequestHeader 访问特定的传入请求标头。
你可以使用内置的 useRequestHeader
可组合项来访问页面、组件和插件中的任何传入请求标头。
¥You can use the built-in useRequestHeader
composable to access any incoming request header within your pages, components, and plugins.
// Get the authorization request header
const authorization = useRequestHeader('authorization')
在浏览器中,
useRequestHeader
将返回 undefined
。示例
¥Example
我们可以使用 useRequestHeader
轻松判断用户是否获得授权。
¥We can use useRequestHeader
to easily figure out if a user is authorized or not.
以下示例读取 authorization
请求标头以确定用户是否可以访问受限资源。
¥The example below reads the authorization
request header to find out if a person can access a restricted resource.
middleware/authorized-only.ts
export default defineNuxtRouteMiddleware((to, from) => {
if (!useRequestHeader('authorization')) {
return navigateTo('/not-authorized')
}
})