如何在angular4项目中导入angular2-cookie

How to import angular2-cookie in angular4 project

我正在尝试在我的 angular4 项目中导入 angular2-cookie。

我的项目是用angular-cli@1.2.7构建的,这里是我的package.json:

{
  "name": "ng-admin",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve --port 4201",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^4.0.0",
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "angular2-cookie": "^1.2.6",
    "core-js": "^2.4.1",
    "rxjs": "^5.1.0",
    "zone.js": "^0.8.4"
  },
  "devDependencies": {
    "@angular/cli": "^1.2.7",
    "@angular/compiler-cli": "^4.0.0",
    "@types/jasmine": "2.5.38",
    "@types/node": "~6.0.60",
    "codelyzer": "~2.0.0",
    "jasmine-core": "~2.5.2",
    "jasmine-spec-reporter": "~3.2.0",
    "karma": "~1.4.1",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^0.2.0",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.0",
    "ts-node": "~2.0.0",
    "tslint": "~4.5.0",
    "typescript": "~2.2.0"
  }
}

angular2-cookie dochttps://github.com/salemdar/angular2-cookie#cli 要求我在 angular-cli-build.js 中添加一些内容,但我既没有 angular-cli-build.js 也没有 systemjs.config.js

我将 angular2-cookie 导入 app.module.ts 中:

import { CookieService } from 'angular2-cookie/services/cookies.service';
...
...
providers: [
    CookieService
 ]

我的项目在 ng server 中运行良好,在我的 chrome 中也运行良好。

它是用 ng build --prod 成功构建的,但是当我 运行 它在 chrome 或 运行 它与 ng server --aot , chrome控制台显示此错误:

ERROR Error: No provider for CookieOptions! 

我该如何解决这个问题?谢谢大家!

在导入语句中添加 CookieOptions,例如 import { CookieService, CookieOptions } from 'angular2-cookie';然后在根模块的提供者数组中添加 CookieService 和 CookieOptions。