Angualr2使用Cookie

Angualr2中提供了使用Cookie的官方库angular2-cookie

1. 如何安装呢?

使用npm安装:
打开终端cd到项目中,运行下面命令:

npm install angular2-cookie --save

安装完成后在systemjs.config.js文件中的map下最后一行添加如下代码:

'angular2-cookie':            'npm:angular2-cookie'

注:别忘了在上一行的末尾添加逗号“,”。
packages的最后一行添加如下代码:

'angular2-cookie': {
    main: './core.js',
    defaultExtension: 'js'
}

注:同样的,别忘了在上一行的末尾添加逗号“,”。
如果你没有改动过官方的快速起步,那么现在你的systemjs.config.js文件看起来应该是这样婶儿的:

(function (global) {
    System.config({
        paths: {
            // paths serve as alias 
            'npm:': 'node_modules/'
        },
        // map tells the System loader where to look for things 
        map: {
            // our app is within the app folder 
            app: 'app',

            // angular bundles 
            '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
            '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
            '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
            '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
            '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
            '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
            '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

            // other libraries 
            'rxjs':                       'npm:rxjs',
            'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
            'angular2-cookie':            'npm:angular2-cookie'
        },
        // packages tells the System loader how to load when no filename and/or no extension 
        packages: {
            app: {
                main: './main.js',
                defaultExtension: 'js'
            },
            rxjs: {
                defaultExtension: 'js'
            },
            'angular2-in-memory-web-api': {
                main: './index.js',
                defaultExtension: 'js'
            },
            'angular2-cookie': {
                main: './core.js',
                defaultExtension: 'js'
            }
        }
    });
})(this);

2. 如何使用呢?

首先引用Angular2-Cookie:

import { CookieService } from 'angular2-cookie/services/cookies.service';

将Angular2-Cookie服务注入:

constructor(private _cookieService:CookieService){}

便可以调用了。

3. 都有哪些方法呢?

get(key: string): string

通过一个String类型的key获取一个String类型的数据。

getObject(key: string): Object

通过一个String类型的key获取一个Object类型的数据。

getAll(): any

获取所有Cookie中保存的内容。

put(key: string, value: string, options?: CookieOptionsArgs): void

保存string类型的数据。

putObject(key: string, value: Object, options?: CookieOptionsArgs): void

保存Object类型的数据。

remove(key: string, options?: CookieOptionsArgs): void

删除指定的数据。

removeAll(): void

删除所有数据。

注:
CookieOptionsArgs:

  • path - [string] - 保存路径,保存的数据只适用于输入路径及其子路径,路径是基于<base>标签中的路径。
  • domain - [string] - 保存域,保存的数据只适用于当前域及其子域,
  • expires - [string|Data] - "Wdy, DD Mon YYYY HH:MM:SS GMT"样式的字符串或者Data类型,设置保存数据的到期日期。
  • secure - [boolean] - 如果设置为true,数据只能通过安全连接获取。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,785评论 18 399
  • 类型没要求。活动链接
    Jack叔叔阅读 487评论 1 0
  • 一个人的时候 我以屋子为世界 灯下影,影上灯 从入夜到深更 半开着的窗透过窸窣虫鸣 楼外霓虹怂恿着内心汹涌 还有,...
    枕藏写影视阅读 387评论 0 3
  • 使用纯html实现考试页面(不使用任何css/js),使用正确合适的标签做出如下的页面效果。内容需要完全一样,但页...
    small柚子君阅读 1,225评论 0 2