useJwt
jwt-decode
的封装。
Demo
Header
{
"alg": "HS256",
"typ": "JWT"
}
Payload
{
"sub": "1234567890",
"iat": 1516239022
}
安装
bash
npm install jwt-decode@^4
使用方法
typescript
import { useJwt } from '@vueuse/integrations/useJwt'
import { defineComponent } from 'vue'
export default defineComponent({
setup() {
const encodedJwt = ref('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyfQ.L8i6g3PfcHlioHCCPURC9pmXT7gdJpx3kOoyAfNUwCc')
const { header, payload } = useJwt(encodedJwt)
return { header, payload }
},
})
类型声明
typescript
export interface UseJwtOptions<Fallback> {
/**
* 解码出错时返回的值
*
* @default null
*/
fallbackValue?: Fallback
/**
* 解码出错时的错误回调函数
*/
onError?: (error: unknown) => void
}
export interface UseJwtReturn<Payload, Header, Fallback> {
header: ComputedRef<Header | Fallback>
payload: ComputedRef<Payload | Fallback>
}
/**
* 响应式解码的 JWT token。
*
* @see https://vueuse.org/useJwt
*/
export declare function useJwt<
Payload extends object = JwtPayload,
Header extends object = JwtHeader,
Fallback = null,
>(
encodedJwt: MaybeRefOrGetter<string>,
options?: UseJwtOptions<Fallback>,
): UseJwtReturn<Payload, Header, Fallback>
Source
贡献者
Anthony Fu
丶远方
Anthony Fu
Devix Rootix
Doctorwu
Curt Grimes
Jelf
Alex Kozack
lstoeferle
变更日志
v11.0.0-beta.1
on 6/12/2024v10.0.0-beta.4
on 4/13/20234d757
- feat(types)!: rename MaybeComputedRef
to MaybeRefOrGetter
0a72b
- feat(toValue): rename resolveUnref
to toValue