Angular service to read, set and delete browser cookies. Originally based on the ng2-cookies library. The experienced team behind Studytube will take care of our cookie service from now on.
npm install ngx-cookie-service --save
# or
yarn add ngx-cookie-service
Add the cookie service to your app.module.ts
as a provider:
import { CookieService } from 'ngx-cookie-service';
@NgModule({
...
providers:
[CookieService],
...
})
export class AppModule {
}
Then, import and inject it into a constructor:
constructor(private
cookieService: CookieService
)
{
this.cookieService.set('Test', 'Hello World');
this.cookieValue = this.cookieService.get('Test');
}
That's it!
https://stackblitz.com/edit/angular-ivy-1lrgdt?file=src%2Fapp%2Fapp.component.ts
For Angular 9.0.0+ use latest version of the library. For versions < 9.x.x, use 2.3.0 version
Angular Version | Supported Version |
---|---|
9.0.0+ | latest |
< 9.0.0 | 2.3.0 |
const cookieExists: boolean = cookieService.check('test');
Checks if a cookie with the givenname
can be accessed or found.
const value: string = cookieService.get('test');
Gets the value of the cookie with the specified name
.
const allCookies: {} = cookieService.getAll();
Returns a map of key-value pairs for cookies that can be accessed.
cookieService.set('test', 'Hello World');
cookieService.set('test', 'Hello World', { expires: 2, sameSite: 'Lax' });
Sets a cookie with the specified name
and value
. It is good practice to specify a path. If you are unsure about thepath value, use '/'
. If no path or domain is explicitly defined, the current location is assumed. sameSite
defaultsto Lax
.
Important: For security reasons, it is not possible to define cookies for other domains. Browsers do not allow this.Read this and this StackOverflowanswer for a more in-depth explanation.
Important: Browsers do not accept cookies flagged sameSite = 'None' if secure flag isn't set as well. CookieServicewill override the secure flag to true if sameSite='None'.
cookieService.delete('test');
Deletes a cookie with the specified name
. It is best practice to always define a path. If you are unsure about thepath value, use '/'
.
Important: For security reasons, it is not possible to delete cookies for other domains. Browsers do not allow this.Read this and this StackOverflowanswer for a more in-depth explanation.
cookieService.deleteAll();
Deletes all cookies that can currently be accessed. It is best practice to always define a path. If you are unsure aboutthe path value, use '/'
.
Checking out the following resources usually solves most of the problems people seem to have with this cookie service:
The following general steps are usually very helpful when debugging problems with this cookie service or cookies ingeneral:
document.cookie
document.cookie
manually (i.e. in a console of your choice)?Package managers are a well known source of frustration. If you have "token missing" or "no provider" errors, a simplere-installation of your node modules might suffice:
rm -rf node_modules
yarn # or `npm install`
Please be aware that we cannot help you with problems that are out of scope. For example, we cannot debug a Symfony orSpringboot application for you. In that case, you are better off asking the nice folks overat StackOverflow for help.
There is an issue for that. Checkout this comment for more informationabout future support.
Please make sure to check out our FAQ before you open a new issue. Also, try to give us as much information as you canwhen you open an issue. Maybe you can even supply a test environment or test cases, if necessary?
We are happy to accept pull requests or test cases for things that do not work. Feel free to submit one of those.
However, we will only accept pull requests that pass all tests and include some new ones (as long as it makes sense toadd them, of course).
This cookie service is brought to you by 7leads GmbH. We built it for one of our apps, becausethe other cookie packages we found were either not designed "the Angular way" or caused trouble during AOT compilation.
Thanks to all contributors:
The POST action seems to have no effect on the .net core controller. If you put [IgnoreAntiforgeryToken] attribute on the server controller method, it works. But dont' leave it like that exposing vuln
封装初衷-不想每次设置cookie的过期时间都写一遍,比较烦! 目前这个包放在npm库中,下载npm install happycookie class happyCookie { constructor() { this["_instruction"] = { "存cookie": ` 1.参数说明 1.1cookieName:设置co
openresty ngx_lua常用指令 请求中断 ngx.exit:请求中断 语法格式:ngx.exit(status) * status >= 200:立刻中断请求,返回状态码 * status == 0:中断当前执行阶段,继续执行后续
Ngx指令 lua_code_cache on | off; 作用:打开或关闭 Lua 代码缓存,影响以下指令: set_by_lua_file , content_by_lua_file, rewrite_by_lua_file, access_by_lua_file 及强制加载或者reload Lua 模块等.缓存开启时修改LUA代码需要重启nginx,不开启时则不用。开发阶段一般关闭缓存。
1.安装ngx-cookie-service 关闭项目,在终端输入 npm install ngx-cookie-service 2.安装完在你项目所需要的component组件里面的typescript文件里面引入并且声明 2.1 引入 import { CookieService } from "ngx-cookie-service"; 2.2 声明(在构造函数声明) construct
https://github.com/stevermeister/ngx-cookie-service
ngx-weui 是一个使用 Angular 构建的 WeUI 组件。 在线示例以及API文档。
ngx-fastdfs 是 nginx + lua +fastdfs 实现分布式图片实时动态压缩。 install 进入docker目录docker build -t fastdfs:dev . 使用 docker -idt -p 80:80 fastdfs:dev /bin/bash进入容器执行/etc/rc.local 测试 进入容器执行test目录下的./test.sh或者直接执行下面脚本
ngx-markdown ngx-markdown is an Angular library that combines... Marked to parse markdown to HTML Prism.js for language syntax highlight Emoji-Toolkit for emoji support KaTeX for math expression rende
ngx-admin Who uses ngx-admin?| Documentation | Installation Guidelines | Angular templates New! Material theme for ngx-admin Material admin theme is based on the most popular Angular dashboard templat
@sweetalert2/ngx-sweetalert2 Official SweetAlert2 integration for Angular This is not a regular API wrapper for SweetAlert (which already works very well alone), it intends to provide Angular-esque ut
ngx-dropzone A lightweight and highly customizable Angular dropzone component for file uploads. For a demo see DEMO. And the CODE for the demo. Install $ npm install --save ngx-dropzone Usage // in ap