How to use Angular2 with ng2 cookie

GET http: // localhost / ng2-cookies / ng2-cookies 404 (not found)

Here is my error log.

I am trying to use the ng2-cookie module in my project.

Here is app.component.ts

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

What is the problem? Please, help

+7
angular
source share
6 answers

I had a similar problem. Solved it by making the following changes to the system.config.js file and it worked -

  • Add ng2-cookie to record the card 'ng2-cookies': 'node_modules / ng2-cookies'

  • Add ng2 cookies to the 'ng2 cookies' batch entry: {defaultExtension: 'js'}

+7
source share

try it

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

We have no method like:

 setCookie and getCookie in Js class Extract from JS class /** * Retrieves a single cookie by it name * * @param {string} name Identification of the Cookie * @returns The Cookie 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 identification * @param {string} value Cookie value * @param {number} expires Cookie expiration date in days from now. If it 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
source share

I think you should add a map entry to the SystemJS configuration file. Something like that:

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

Try using this.

 import { Cookie }` from `'ng2-cookies/ng2-cookies'; Cookie.set('ticket_status', responses.data.ticket); 
+1
source share

This is what worked for me.

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

Like some other answers ... but this is what worked for me (Angular 4.0.3 (there was no way to upgrade to 5):

 // systemjs.config.js map: { ... 'ng2-cookies' : 'node_modules/ng2-cookies', } packages: { ... 'ng2-cookies': {defaultExtension: 'js', main:'./index.js'} } // my app module import { CookieService } from 'ng2-cookies'; @NgModule({ ... providers: [ ... CookieService ] }) // my components import { CookieService } from 'ng2-cookies'; constructor(private cookieService:CookieService){} 
0
source share

All Articles