試試這個
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;
}
任何錯誤信息?期望的和實際的行爲是什麼? –
它沒有任何錯誤信息。它說:「在終端上。15:07:51 - 編譯完成。觀察文件變化。」 –
@МагнайбаярГанзориг,你解決這個問題?我面臨同樣的問題,我試過這個(https://github.com/mgechev/angular2-seed/wiki/Add-external-dependency),但它也沒有工作。 – dasnervtdoch