2016-02-29 71 views
7

GET http://localhost/ng2-cookies/ng2-cookies 404(未找到)如何使用Angular2和ng2-cookie

這是我的錯誤日誌。

我想在我的項目上使用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')); 

什麼樣的問題?請幫助

+0

任何錯誤信息?期望的和實際的行爲是什麼? –

+0

它沒有任何錯誤信息。它說:「在終端上。15:07:51 - 編譯完成。觀察文件變化。」 –

+0

@МагнайбаярГанзориг,你解決這個問題?我面臨同樣的問題,我試過這個(https://github.com/mgechev/angular2-seed/wiki/Add-external-dependency),但它也沒有工作。 – dasnervtdoch

回答

1

我認爲你應該添加一個map條目到你的SystemJS配置文件中。類似的東西:

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

我遇到了類似的問題。這樣做在system.config.js文件以下更改解決了它和它的工作 -

  1. 添加NG2餅乾映射條目 「NG2餅乾」:「node_modules/NG2餅乾」

  2. 添加NG2-餅乾包裝進入 'NG2餅乾':{defaultExtension: 'JS'}

+0

我不得不添加'main:'./ index.js''到包,所以看起來更像'ng2-cookies':{main:'./index.js',defaultExtension:'js'}'。我應該在這裏回答,而不是張貼我自己的答案 - 哈哈!以我的道歉=) –

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; 
    } 
+0

爲什麼此評論有-1? –

1

嘗試使用此。

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

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

這是對我工作。

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

到一些其他的答案相似...但這是我工作(角4.0.3(還沒有來得及更新到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){}