You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Redux中间件为Cookie添加Path属性?

在redux-cookies-middleware中为Cookie添加path属性的方法

嘿,关于redux-cookies-middleware添加Cookie的path属性这件事,我刚好有经验!你目前的配置只用到了name字段,其实这个middleware支持直接在每个Cookie的配置项里添加所有标准的Cookie属性,包括path哦~

你只需要在对应的Cookie配置对象里直接新增path键值对就行,比如把你原来的配置改成这样:

{
  session: {
    name: 'session', // name of the cookie in which the value of session will be synced
    path: '/' // 给session Cookie设置根路径
  },
  'auth.token': {
    name: 'auth_token', // name of the cookie in which the value of auth.token will be synced
    path: '/api' // 给auth.token Cookie设置/api路径
  }
}

除了path之外,像expires、domain、secure这些标准Cookie属性,也可以用同样的方式添加到配置里。比如想要设置Cookie过期时间,可以加上expires: new Date(Date.now() + 86400000)(24小时后过期),middleware会自动处理这些参数并生成对应的Cookie。

内容的提问来源于stack exchange,提问作者Serhii Minin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:10:54