refreshCookie

当 Cookie 发生变化时手动刷新 useCookie 值
此实用程序自 Nuxt v3.10 起可用。

用途

¥Purpose

refreshCookie 函数旨在刷新 useCookie 返回的 Cookie 值。

¥The refreshCookie function is designed to refresh cookie value returned by useCookie.

当我们知道新的 Cookie 值已在浏览器中设置时,这对于更新 useCookie 引用很有用。

¥This is useful for updating the useCookie ref when we know the new cookie value has been set in the browser.

用法

¥Usage

app.vue
<script setup lang="ts">
const tokenCookie = useCookie('token')

const login = async (username, password) => {
  const token = await $fetch('/api/token', { ... }) // Sets `token` cookie on response
  refreshCookie('token')
}

const loggedIn = computed(() => !!tokenCookie.value)
</script>
你可以启用实验性的 cookieStore 选项,以便在浏览器中的 Cookie 发生变化时自动刷新 useCookie 值。¥You can enable experimental cookieStore option to automatically refresh useCookie value when cookie changes in the browser.

类型

¥Type

refreshCookie(name: string): void