CapacitorCookies
Capacitor Cookies API 通过修补 document.cookie 以使用原生库的方式,提供原生 Cookie 支持。它还提供了在特定 URL 修改 Cookie 的方法。该插件已捆绑在 @capacitor/core 中。
配置
默认情况下,修补 document.cookie 以使用原生库的功能是禁用的。
如果你想启用此功能,请在 capacitor.config 文件中修改以下配置。
| 属性 | 类型 | 描述 | 默认值 |
|---|---|---|---|
enabled | boolean | 启用修补 document.cookie,使其使用原生库而非默认实现。 | false |
配置示例
在 capacitor.config.json 中:
{
"plugins": {
"CapacitorCookies": {
"enabled": true
}
}
}
在 capacitor.config.ts 中:
import { CapacitorConfig } from '@capacitor/cli';
const config: CapacitorConfig = {
plugins: {
CapacitorCookies: {
enabled: true,
},
},
};
export default config;
示例
import { CapacitorCookies } from '@capacitor/core';
const getCookies = () => {
return document.cookie;
};
const setCookie = () => {
document.cookie = key + '=' + value;
};
const setCapacitorCookie = async () => {
await CapacitorCookies.setCookie({
url: 'http://example.com',
key: 'language',
value: 'en',
});
};
const deleteCookie = async () => {
await CapacitorCookies.deleteCookie({
url: 'https://example.com',
key: 'language',
});
};
const clearCookiesOnUrl = async () => {
await CapacitorCookies.clearCookies({
url: 'https://example.com',
});
};
const clearAllCookies = async () => {
await CapacitorCookies.clearAllCookies();
};