如何将Angular2与ng2-cookie一起使用

时间:2016-02-29 07:05:21

标签: angular

获取http://localhost/ng2-cookies/ng2-cookies 404(未找到)

这是我的错误日志。

我试图在我的项目中使用ng2-cookies模块。

这是app.component.ts

import {Cookie} from 'ng2-cookies/ng2-cookies';
Cookie.setCookie('ticket_status', responses.data.ticket);
console.log(Cookie.getCookie('ticket_status'));

什么样的问题?请帮忙

6 个答案:

答案 0 :(得分:7)

我遇到了类似的问题。通过在system.config.js文件中进行以下更改解决了这个问题并且它起了作用 -

  1. 将ng2-cookies添加到地图条目中 'ng2-cookies':'node_modules / ng2-cookies'

  2. 将ng2-cookies添加到包条目中 'ng2-cookies':{defaultExtension:'js'}

答案 1 :(得分:2)

试试这个

Cookie.set('ticket_status', "hello");
console.log(Cookie.get('ticket_status'));

我们没有类似的方法:

setCookie and getCookie in Js class

Extract from JS class


    /**
     * Retrieves a single cookie by it's name
     *
     * @param  {string} name Identification of the Cookie
     * @returns The Cookie's value
     */
    public static get(name: string): string  {
        if (Cookie.check(name)) {
            name = encodeURIComponent(name);
            let regexp = new RegExp('(?:^' + name + '|;\\s*' + name + ')=(.*?)(?:;|$)', 'g');
            let result = regexp.exec(document.cookie);
            return decodeURIComponent(result[1]);
        } else {
            return '';
        }   
    }


/**
     * Save the Cookie
     *
     * @param  {string} name Cookie's identification
     * @param  {string} value Cookie's value
     * @param  {number} expires Cookie's expiration date in days from now. If it's undefined the cookie is a session Cookie
     * @param  {string} path Path relative to the domain where the cookie should be avaiable. Default /
     * @param  {string} domain Domain where the cookie should be avaiable. Default current domain
     * @param  {boolean} secure If true, the cookie will only be available through a secured connection
     */
    public static set(name: string, value: string, expires?: number, path?: string, domain?: string, secure?: boolean) {
        let cookieStr = encodeURIComponent(name) + '=' + encodeURIComponent(value) + ';';

        if (expires) {
            let dtExpires = new Date(new Date().getTime() + expires * 1000 * 60 * 60 * 24);
            cookieStr += 'expires=' + dtExpires.toUTCString() + ';';
        }
        if (path) {
            cookieStr += 'path=' + path + ';';
        }
        if (domain) {
            cookieStr += 'domain=' + domain + ';';
        }
        if (secure) {
            cookieStr += 'secure;';
        }

        // console.log(cookieStr);
        document.cookie = cookieStr;
    }

答案 2 :(得分:1)

我认为您应该在SystemJS配置文件中添加map条目。这样的事情:

<script>
  System.config({
    defaultJSExtensions: true,
    map: {
      "ng2-cookies": 'node_modules/ng2-cookies'
    },
    packages: {
      (...)
    }
  });
</script>

答案 3 :(得分:1)

尝试使用它。

import { Cookie }` from `'ng2-cookies/ng2-cookies';

Cookie.set('ticket_status', responses.data.ticket);

答案 4 :(得分:0)

这对我有用。

map: {
    ...,
    'ng2-cookies': 'npm:ng2-cookies'
},
packages: {
    ...,
    'ng2-cookies': {
      main: './index.js',
      defaultExtension: 'js'
    },
}

答案 5 :(得分:0)

与其他一些答案类似......但这对我有用(Angular 4.0.3(没有机会更新到5):

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: <CERTIFICATE_DATA>
    server: <IP_ADDRESS>
  name: <cluster_name>
contexts:
- context:
    cluster: <cluster_name>
    user: <cluster_name>
    name:  <cluster_name>
users:
- name: <cluster_name>
  user:
    auth-provider:
      config:
        access-token: <SOME_ACCESS_TOKEN>
        cmd-args: config config-helper --format=json
        cmd-path: /usr/lib/google-cloud-sdk/bin/gcloud
        expiry: 2017-11-10T03:20:19Z
        expiry-key: '{.credential.token_expiry}'
        token-key: '{.credential.access_token}'
      name: gcp
相关问题