迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > Angular >

在 Angular 中设置 Cookie

作者:迹忆客 最近更新:2023/03/13 浏览次数:

在本文中,我们将讨论 Angular 中的 cookie。我们将通过示例介绍如何在 Angular 中设置 cookie。

Cookie 是通常由你的浏览器存储的小型详细信息包,并且网站倾向于将 Cookie 用于许多事情。Cookies 在不同的请求中持续存在,浏览器会话应该可以帮助我们设置它们,它们可以成为一些 Web 应用程序中的一种很好的身份验证技术。

我们可以在很多情况下使用 cookie 来存储重要数据,例如用于身份验证或登录时保存用户详细信息。我们可以随时轻松地从 cookie 中检索数据。

通常,我们每个 Web 服务器只能存储 20 个 cookie,每个 cookie 中的数据不超过 4KB,如果我们更愿意确定 max-age 属性,它们可以无限期地保存。

让我们使用以下命令在 Angular 中创建一个新应用程序。

ng new my-app

在 Angular 中创建我们的新应用程序后,我们将使用此命令转到我们的应用程序目录。

cd my-app

让我们运行我们的应用程序来检查所有依赖项是否都安装正确。

ng serve --open

我们需要使用库 ngx-cookie-service 在 Angular 中设置 cookie。

Angular 提供了一个库 ngx-cookie-service,可用于在我们的 Web 应用程序中设置 cookie。我们可以使用以下命令轻松安装此库。

npm install ngx-cookie-service --save

此命令将下载并安装我们项目的 node_modules 文件夹中的 ngx-cookie-service 库,它也将包含在依赖项中。

一旦我们成功地将这个库添加到我们的项目中,我们就可以设置 cookie。建议我们在模块文件中导入 CookieService,然后将其添加到我们的提供者的数组中,如下所示。

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { CookieService } from 'ngx-cookie-service';
import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';

@NgModule({
  imports: [BrowserModule, FormsModule],
  declarations: [AppComponent, HelloComponent],
  bootstrap: [AppComponent],
  providers: [CookieService],
})
export class AppModule {}

一旦我们将它添加为提供者,我们就可以在我们的 app.component.ts 之一中使用它,如下所示。

import { Component } from '@angular/core';
import { CookieService } from 'ngx-cookie-service';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  constructor(private cookieService: CookieService) {}
  ngOnInit() {
    this.cookieService.set('Test', 'Hasnain');
  }

  getCookie() {
    const value: string = this.cookieService.get('Test');
    console.log(value);
  }
}

输出:

在 Angular 中设置 cookie

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便