无法解析ApplicationModule的参数:(?)

时间:2018-10-21 07:43:08

标签: angular angular-services angular-dependency-injection

Angular的新手。 我的应用程序包含1个服务和3个组件。编译成功。 我收到此错误,不知道出了什么问题:

  

未捕获的错误:无法解析ApplicationModule的所有参数:(?)。

调试给我的想法很少。.问题似乎与具有null依赖项的NSLocaleLocalizations有关(如果有意义)-请参见下面的屏幕截图: enter image description here

这是一些代码: 请让我知道是否还有其他需要。 非常感谢您的帮助-非常感谢。

package.json

{
  "name": "angular_app",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "build-aot": "ng build --prod --aot",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
  "@angular/animations": "^6.0.0",
  "@angular/cdk": "^6.0.0",
  "@angular/common": "^6.0.0",
  "@angular/compiler": "^6.0.0",
  "@angular/core": "^6.0.0",
  "@angular/forms": "^6.0.0",
  "@angular/http": "^6.0.0",
  "@angular/material": "^6.0.0",
  "@angular/platform-browser": "^6.0.0",
  "@angular/platform-browser-dynamic": "^6.0.0",
  "@angular/router": "^6.0.0",
  "core-js": "^2.5.4",
  "express": "^4.16.4",
  "rxjs": "^6.0.0",
  "zone.js": "^0.8.26"
  },
  "devDependencies": {
  "@angular/compiler-cli": "^6.0.0",
  "@angular-devkit/build-angular": "~0.6.0",
  "typescript": "~2.7.2",
  "@angular/cli": "~6.0.0",
  "@angular/language-service": "^6.0.0",
  "@types/jasmine": "~2.8.6",
  "@types/jasminewd2": "~2.0.3",
  "@types/node": "~8.9.4",
  "codelyzer": "~4.2.1",
  "jasmine-core": "~2.99.1",
  "jasmine-spec-reporter": "~4.2.1",
  "karma": "~1.7.1",
  "karma-chrome-launcher": "~2.2.0",
  "karma-coverage-istanbul-reporter": "~1.4.2",
  "karma-jasmine": "~1.1.1",
  "karma-jasmine-html-reporter": "^0.2.2",
  "protractor": "~5.3.0",
  "ts-node": "~5.0.1",
  "tslint": "~5.9.1"
  }

}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import { StoriesComponent } from './pages/stories/stories.component';
import { StoryComponent } from './pages/story/story.component';
import { StageComponent } from './pages/stage/stage.component';

@NgModule({
declarations: [
  AppComponent,
  StoryComponent,
  StoriesComponent,
  StageComponent
],
imports: [
  BrowserModule,
  AppRoutingModule,
  HttpModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { StoriesComponent } from './pages/stories/stories.component';
import { StoryComponent } from './pages/story/story.component';
import { StageComponent } from './pages/stage/stage.component';

const routes: Routes = [
  {path: '', redirectTo: '/stories', pathMatch: 'full'},
  {path: 'stories', component:StoriesComponent},
  {path: 'stories/:id', component: StoryComponent},
  {path: 'stories/:id/:id', component: StageComponent}
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

stories.service.ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { map, tap } from 'rxjs/operators';

import { BehaviorSubject } from 'rxjs';

@Injectable({
  providedIn: 'root'
})

export class StoriesService {


  private _stories = new BehaviorSubject<any>(null);
  public stories = this._stories.asObservable();


  storyIdCounter = 0;
  stageIdCounter = 0;
  stateIdCounter = 0;

  constructor(private http: Http) { 
    this.getStoriesForReal().subscribe(
      (stories) => {
        console.log(stories);
        this._stories.next(stories);
      });
  }

  getStoriesForReal() {
    return this.http.get("http://localhost:3001/test").pipe(
      tap(res => console.log(res)),
      map((response) => response.json())
    );
  }

}

stories.component.ts

import { Component, OnInit } from '@angular/core';
import { Story } from '../../interfaces/story';
import { StoriesService } from '../../services/stories.service';


@Component({
  selector: 'app-stories',
  templateUrl: './stories.component.html',
  styleUrls: ['./stories.component.scss']
})
export class StoriesComponent implements OnInit {

  id = 0;
  storiesList: Story[];

  constructor(
    private storiesService: StoriesService) { 
    }

  ngOnInit() {
    this.storiesList = [];
    this.storiesService.stories.subscribe((stories) => {
      for (let story in stories) {
        this.storiesList.push(stories[story]);
      }
    });
  }  
}

story.component.ts

import { Component, OnInit } from '@angular/core';
import { StoriesService } from '../../services/stories.service';


@Component({
  selector: 'app-story',
  templateUrl: './story.component.html',
  styleUrls: ['./story.component.scss']
})
export class StoryComponent implements OnInit {

  constructor(
    private storiesService: StoriesService
  ) { 

  }

  ngOnInit() {

  }
}

stage.component.ts

import { Component, OnInit } from '@angular/core';

import { StoriesService } from '../../services/stories.service';

@Component({
  selector: 'app-stage',
  templateUrl: './stage.component.html',
  styleUrls: ['./stage.component.scss'],
})
export class StageComponent implements OnInit {

  constructor(
    private storiesService: StoriesService
  ) { }


  ngOnInit() {

  }
}

2 个答案:

答案 0 :(得分:0)

您需要在AppModule中提供StoriesService

  ....
  providers: [StoriesService],
  bootstrap: [AppComponent]
  .... 

此外,使用HttpClientModule代替HttpClient

答案 1 :(得分:0)

我得到这个的原因是我不正确地从Angular 5.2升级到Angular7。请转到https://update.angular.io/了解如何从任何版本升级到另一个版本

编辑:

也请看一下https://github.com/nwjs/nw.js/issues/6804,如果您使用的是NWJS,这是一个解决方案

对于使用NWJS和Angular 6并需要像我一样的Node功能的任何人,在@rogerwang将我引到此链接之后,我都可以进行纠正。但是,如果需要沿侧面角度使用Node功能,则最肯定的建议可能不会起作用。在加载应用程序之前,角度编译器中会发生两个Node REQUIRE定义(Angular 6和NWJS)中的冲突,因此通常在index.html文件或polyfill.ts中添加脚本不会解决此问题。要解决此问题,请在您的nw清单中,不要删除node-remote字段或nodejs标志,不要添加新的字段bg-script并通过上面链接中指定的解决方法表达式传递js文件。在Angular应用中,在nw对象中重新分配require函数。解决了。参见下面的示例

removeRequire.js

window.requireNode = window.require;
window.require = undefined;

NW package.json

{"name": "angular-app",
  "version": "0.0.0",
  "main": "http://localhost:4200/",
  "node-remote": "http://localhost:4200",
  "bg-script": "removeRequire.js",
  "window": {
    "toolbar": false,
    "title": "App",
    "width": 550,
    "height": 870
  },
  "scripts": {
    "start": "nw ."
  }
}

Angular 6应用程序polyfill.ts

declare var nw: any;
const isNWjsAvailable = typeof (nw) === 'object';

(function(inkioskMode: boolean) {
    if (inkioskMode) {
        nw.require = nw.global.require =  nw.global.requireNode;
        nw.global.requireNode = undefined;
    }
}(isNWjsAvailable));

注意 如果您需要通过nw信息亭提供节点功能,这将很有用。如果不需要它,只需删除node-remote字段即可。另外,为bg-script字段提供的脚本应与您的nw清单位于同一文件夹中。如果构建或服务AOT,也可能不需要它。