refreshCookie

当 cookie 发生更改时,手动刷新 useCookie 的值
此工具自 Nuxt v3.10 起可用。

用途

refreshCookie 函数用于刷新 useCookie 返回的 cookie 值。

当我们知道浏览器中已设置了新的 cookie 值时,此功能可用于更新 useCookie 的 ref。

使用方法

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

const login = async (username, password) => {
  const token = await $fetch('/api/token', { ... }) // 在响应中设置 `token` cookie
  refreshCookie('token')
}

const loggedIn = computed(() => !!tokenCookie.value)
</script>
你可以启用实验性的 cookieStore 选项,以在浏览器中的 cookie 更改时自动刷新 useCookie 的值。

类型

refreshCookie(name: string): void