我正在制作一个可组合项,它返回有关用户的数据。 我想在对远程服务器的请求中获取用户状态(已验证或访客)。我将访问令牌存储在 cookie 中,并以这种方式发出此请求:
// whether the user is authenticated
const isAuthenticated: ComputedRef<boolean> = computed((): boolean => {
// validate auth token
if ('undefined' === typeof authToken.value || null === authToken.value) {
return false;
}
// get app config
const appConfig: any = useAppConfig();
// make the request
const { error } = useAsyncData<ServerResponseData, ServerResponseError>(
'user-check-auth-token', () => $fetch(`${appConfig.api.baseUrl}/user/check-auth-token`, {
method: 'post',
headers: {
Authorization: `Bearer ${authToken.value}`
}
})
);
// token is valid if no error happened
return !error.value;
});
此代码工作正常,但在控制台中我看到此警告:
[Vue warn] Computed is still dirty after getter evaluation, likely because a computed is mutating its own dependency in its getter. State mutations in computed getters should be avoided...
如果您从客户端 cookie 访问数据,则您不希望这在服务器端起作用。在这种情况下,
useAsyncData
在这里可能没有意义。