如何在angular4项目

时间:2017-08-07 14:18:49

标签: javascript angular angular2-cookie

我正在尝试在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 doc https://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! 

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

1 个答案:

答案 0 :(得分:2)

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