如何使用Redux中间件为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
相关产品推荐
相关产品推荐

