ERROR 错误:StaticInjectorError(AppModule)[StorieControllerComponent -> DataService]:
Posted
技术标签:
【中文标题】ERROR 错误:StaticInjectorError(AppModule)[StorieControllerComponent -> DataService]:【英文标题】:ERROR Error: StaticInjectorError(AppModule)[StorieControllerComponent -> DataService]: 【发布时间】:2018-08-18 06:10:32 【问题描述】:我收到了这个错误,我已经检查了所有的导入和模块。
我正在尝试访问我的项目中的 JSON
文件并在对象中使用该数据,但我不确定这里有什么问题。我用JSON
很好地定义了我的对象,但我不确定问题出在 Http 上
这是我在控制台中遇到的错误:
这是服务
import storiesStatus from './stories';
import Injectable from '@angular/core';
import NgModule from '@angular/core';
import HttpClientModule from '@angular/common/http';
import Headers, Http, Response, HttpModule from '@angular/http';
const storiesJson = './../json/stories_data_json.json';
@Injectable()
export class DataService
constructor(private http: Http)
getStoriesData(): Promise<storiesStatus[]>
return this.http.get(storiesJson)
.toPromise()
.then(response => response.json() as storiesStatus[])
.catch();
这是我的应用模块:
import BrowserModule from '@angular/platform-browser';
import NgModule from '@angular/core';
import * as Hammer from 'hammerjs';
import 'hammer-timejs';
import HammerGestureConfig, HAMMER_GESTURE_CONFIG from
'@angular/platform-browser';
import Router, NavigationEnd, ActivatedRoute from '@angular/router';
import ChartsModule from 'ng2-charts';
import AppComponent from './app.component';
import AppChildModule from './modules/app-child.module';
import CheckinComponent from
'./modules/controllers/checkin/checkin.component';
import PicturePostComponent from './modules/controllers/picture-
post/picture-post.component';
import StorieControllerComponent from './modules/controllers/storie-
controller/storie-controller.component';
import LoginComponent from './modules/screens/login/login.component';
import router from './modules/routing/app-routing.module';
import FeedComponent from './modules/screens/home/feed/feed.component';
import HomeComponent from './modules/screens/home/home.component';
import WelcomeComponent from
'./modules/screens/welcome/welcome.component';
import SignupComponent from './modules/screens/signup/signup.component';
import HttpClientModule from '@angular/common/http';
import HttpModule from '@angular/http';
import BrowserAnimationsModule from '@angular/platform-
browser/animations';
export class MyHammerConfig extends HammerGestureConfig
overrides = <any>
'swipe': direction: Hammer.DIRECTION_ALL
@NgModule(
declarations: [
AppComponent,
LoginComponent,
HomeComponent,
WelcomeComponent,
SignupComponent
],
imports: [
BrowserModule,
HttpClientModule,
HttpModule,
AppChildModule,
ChartsModule,
BrowserAnimationsModule,
router
],
providers: [
provide: HAMMER_GESTURE_CONFIG,
useClass: MyHammerConfig
],
bootstrap: [AppComponent]
)
export class AppModule
这是我连接到服务的组件:
import DataService from '../../../../assets/services/data.service';
import storiesStatus from '../../../../assets/services/stories';
import Component, OnInit from '@angular/core';
import NgModule from '@angular/core';
import Headers, Http, Response, HttpModule from '@angular/http';
@Component(
selector: 'app-storie-controller',
templateUrl: './storie-controller.component.html',
styleUrls: ['./storie-controller.component.css']
)
@NgModule(
declarations: []
)
export class StorieControllerComponent implements OnInit
public stories: storiesStatus[];
constructor(private data: DataService)
this.stories = [];
ngOnInit()
this.data.getStoriesData().then(storiesRes => console.log(
this.stories = storiesRes));
【问题讨论】:
您需要将 DataService 添加到您的 app.module 的 providers 数组中。 【参考方案1】:在您的 app.module
中,您忘记将 DataService
添加到您的提供程序数组中
【讨论】:
我试过了但是我得到这个错误:未捕获的错误:模块“AppModule”导入的意外值“DataService”。请添加@NgModule 注释。 更新你的应用模块来展示你是怎么做的,你必须确保你只输入providers: [DataService]
您还需要导入它,目前不需要。
哦,我在供应商上少了一个逗号,好吧哈哈哈谢谢!!以上是关于ERROR 错误:StaticInjectorError(AppModule)[StorieControllerComponent -> DataService]:的主要内容,如果未能解决你的问题,请参考以下文章
错误 [ERR_UNHANDLED_ERROR]:未处理的错误。 (“不正确的论点”)
[PHP] error_reporting可以屏蔽Fatal error错误
在 MySQL 中出现 ERROR 1701、ERROR 1452 和 ERROR 1305 错误 - 需要一些专业知识