useCookie
在您的页面、组件和插件中,您可以使用 useCookie
,这是一个支持服务端渲染的组合式 API,用于读取和写入 cookie。
const cookie = useCookie(name, options)
useCookie
仅在 Nuxt 上下文 中有效。useCookie
的 ref 会自动将 cookie 值序列化和反序列化为 JSON。示例
以下示例创建了一个名为 counter
的 cookie。如果该 cookie 不存在,它将初始设置为一个随机值。每当我们更新 counter
变量时,cookie 将相应更新。
<script setup lang="ts">
const counter = useCookie('counter')
counter.value = counter.value || Math.round(Math.random() * 1000)
</script>
<template>
<div>
<h1>计数器: {{ counter || '-' }}</h1>
<button @click="counter = null">重置</button>
<button @click="counter--">-</button>
<button @click="counter++">+</button>
</div>
</template>
refreshCookie
手动刷新 useCookie
值。选项
Cookie 组合式 API 接受多个选项,允许您修改 cookie 的行为。
大多数选项将直接传递给 cookie 包。
maxAge
/ expires
使用这些选项设置 cookie 的过期时间。
maxAge
:指定 number
(以秒为单位)作为 Max-Age
Set-Cookie
属性 的值。给定的数字将通过向下取整转换为整数。默认情况下,未设置最大年龄。
expires
:指定 Date
对象作为 Expires
Set-Cookie
属性 的值。默认情况下,未设置过期时间。大多数客户端会将此视为“非持久性 cookie”,并在退出 Web 浏览器等条件下删除它。
expires
和 maxAge
,cookie 将仅为会话 cookie,并在用户关闭浏览器时被移除。httpOnly
指定 HttpOnly
Set-Cookie
属性 的 boolean
值。为真时,设置 HttpOnly
属性;否则不设置。默认情况下,未设置 HttpOnly
属性。
true
时要小心,因为合规的客户端将不允许客户端 JavaScript 在 document.cookie
中看到该 cookie。secure
指定 Secure
Set-Cookie
属性 的 boolean
值。为真时,设置 Secure
属性;否则不设置。默认情况下,未设置 Secure
属性。
true
时要小心,因为如果浏览器没有 HTTPS 连接,合规的客户端未来将不会把 cookie 送回服务器。这可能导致水合错误。partitioned
指定 Partitioned
Set-Cookie
属性的 boolean
值。为真时,设置 Partitioned
属性;否则不设置。默认情况下,未设置 Partitioned
属性。
domain
指定 Domain
Set-Cookie
属性 的值。默认情况下,未设置域名,大多数客户端会认为 cookie 仅适用于当前域名。
path
指定 Path
Set-Cookie
属性 的值。默认情况下,路径被视为 "默认路径"。
sameSite
指定 SameSite
Set-Cookie
属性 的 boolean
或 string
值。
true
:将SameSite
属性设置为Strict
,强制执行严格的同站策略。false
:不设置SameSite
属性。'lax'
:将SameSite
属性设置为Lax
,执行宽松的同站策略。'none'
:将SameSite
属性设置为None
,用于显式的跨站 cookie。'strict'
:将SameSite
属性设置为Strict
,强制执行严格的同站策略。
有关不同执行级别的更多信息,请参见 规范。
encode
指定用于编码 cookie 值的函数。由于 cookie 值具有有限的字符集(且必须是简单字符串),此函数可用于将值编码为适合 cookie 值的字符串。
默认编码器为 JSON.stringify
+ encodeURIComponent
。
decode
指定用于解码 cookie 值的函数。由于 cookie 值具有有限的字符集(且必须是简单字符串),此函数可用于将先前编码的 cookie 值解码为 JavaScript 字符串或其他对象。
默认解码器为 decodeURIComponent
+ destr。
default
指定返回 cookie 默认值的函数。该函数还可以返回一个 Ref
。
readonly
允许_访问_ cookie 值,但无法设置它。
watch
指定用于 watch cookie ref 数据的 boolean
或 string
值。
true
:将监视 cookie ref 数据变化及其嵌套属性(默认)。shallow
:仅监视 cookie ref 数据的顶级属性变化。false
:不监视 cookie ref 数据变化。
refreshCookie
手动刷新 useCookie
值。示例 1:
<script setup lang="ts">
const user = useCookie(
'userInfo',
{
default: () => ({ score: -1 }),
watch: false
}
)
if (user.value && user.value !== null) {
user.value.score++; // userInfo cookie 不会因这个变化而更新
}
</script>
<template>
<div>用户得分:{{ user?.score }}</div>
</template>
示例 2:
<script setup lang="ts">
const list = useCookie(
'list',
{
default: () => [],
watch: 'shallow'
}
)
function add() {
list.value?.push(Math.round(Math.random() * 1000))
// list cookie 不会因这个变化而更新
}
function save() {
if (list.value && list.value !== null) {
list.value = [...list.value]
// list cookie 会因这个变化而更新
}
}
</script>
<template>
<div>
<h1>列表</h1>
<pre>{{ list }}</pre>
<button @click="add">添加</button>
<button @click="save">保存</button>
</div>
</template>
API 路由中的 Cookie
您可以在服务器 API 路由中使用来自 h3
包的 getCookie
和 setCookie
来设置 cookie。
export default defineEventHandler(event => {
// 读取 counter cookie
let counter = getCookie(event, 'counter') || 0
// 将 counter cookie 增加 1
setCookie(event, 'counter', ++counter)
// 发送 JSON 响应
return { counter }
})