Angular 在服务未捕获错误中注入服务

Angular 在服务未捕获错误中注入服务,angular,typescript,angular-services,Angular,Typescript,Angular Services,我正在尝试将一个服务注入另一个服务中: instagram.service>event.service.ts(主服务) 其中instagram是将注入event.service中的服务 我已在app.module中声明instagram.service.ts 我得到的错误是: 未捕获错误:无法解析Instagram:(?)的所有参数。 在syntaxError(compiler.js:1021) 在CompileMetadataResolver.push../node_modules/@an

我正在尝试将一个服务注入另一个服务中:

  • instagram.service>event.service.ts(主服务) 其中instagram是将注入event.service中的服务
我已在app.module中声明instagram.service.ts

我得到的错误是:

未捕获错误:无法解析Instagram:(?)的所有参数。 在syntaxError(compiler.js:1021) 在CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.\u getDependenciesMetadata (compiler.js:10922) 在CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.\u getTypeMetadata (compiler.js:10815) 在CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.\u getInjectableTypeMetadata (compiler.js:11037) 在CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getProviderMetadata (compiler.js:11046) 在compiler.js:10984 在Array.forEach()处 在CompileMetadataResolver.push../node\u modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.\u getProvidersMetadata (compiler.js:10944) 在CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNgModuleMetadata (compiler.js:10663) 在JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler.\u loadModules (compiler.js:23876)

在运行了一些测试之后,我注意到只有在导入Http组件时才会发生错误(该组件也在app.module上声明)

我是不是遗漏了什么

事件.服务.ts

import { Injectable } from '@angular/core';
import { AngularFireStorage } from '@angular/fire/storage';
import { Event } from './events.model';
import { AngularFireDatabase, AngularFireList } from '@angular/fire/database';
import { Instagram } from './instagram.service';

@Injectable()

export class EventService {
    event: AngularFireList<Event[]>;
    NODE = 'events/';

    constructor(private instagram: Instagram, private db: AngularFireDatabase, private storage: AngularFireStorage) {}

    getAllPictures(tag: string) {
        this.instagram.getAllPictures(tag);
    }
}
import { Http } from '@angular/http';
export class Instagram {
    tag: string;
    private BASE_URL = 'https://www.instagram.com/explore/tags/' + this.tag + '/?__a=1';
    constructor(public http: Http) {}

    getAllPictures(tag: string) {
        this.tag = tag;
        return this.http.get(this.BASE_URL).pipe().subscribe(
            data => {
                console.log(data);
            },
            (error) => {
                console.log(error);
            }
        );
    }

}

您是否已在
AppModule
中导入
HttpModule


请记住,对于Angular 5,您可以使用
HttpClient
,因为
Http
现在已不推荐使用。有关更多信息,请参见:。

您是否已在
AppModule
中导入
HttpModule


请记住,对于Angular 5,您可以使用
HttpClient
,因为
Http
现在已不推荐使用。有关更多信息,请参阅:。

您需要添加
@Injectable()

到您的
Instagram
服务


好锁

您需要添加
@Injectable()

到您的
Instagram
服务


好锁

通常,如果要注入任何内容,它必须具有@Injectable()注释。 该注释基本上标记了angular要跟踪并提供给请求类的类

只是澄清一下(看看你的评论,似乎不清楚Angular DI是如何工作的): 将被注入的类需要@Injectable注释 通过在构造函数中指定@Inject(InjectableClassName)参数,具有的类需要注入元素。 这些东西适用于所有类别

不过有一个警告,这可能是让你困惑的地方: 已通过angular以任何方式“注册”的类(意味着它们具有@Injectable、@Component、@Pipe或@Directive注释)具有其构造函数的快捷方式:它们不需要@Inject(..)参数,但该参数将通过指示类型和access关键字自动解析(和您一样,因为EventService具有@Injectable)。这只是一个“隐式”注入,angular将以相同的方式处理它

我认为这样说很重要,因为类型只是可能的“DI令牌”之一。 例如,我的一个项目中有一个构造函数:

    constructor(private eventService: EventService, @Inject(SERVICE_IMPLEMENTATIONS_MAP) private serviceMap: any) {}
正如您所看到的,serviceMap的类型是any,但我确实通过包含模块中提供的依赖项令牌注入了它,并且我实际上可以向它传递任何东西(我用于不同类型的模拟和没有公共API的实际服务)。正如您所看到的,我也为其他服务使用了快捷方式DI

记住,@Injectable并不意味着它的构造函数可以被注入,它意味着这个类可以被注入其他地方。
有了你的代码,我可以在某个地方编写构造函数(private e:EventService),Angular将为我注入你的EventService(同时再次注入instagram服务)。注入的东西不会注入其他东西,而是注入其他东西。

一般来说,如果你想注入任何东西,它必须有@Injectable()注释。 该注释基本上标记了angular要跟踪并提供给请求类的类

只是澄清一下(看看你的评论,似乎不清楚Angular DI是如何工作的): 将被注入的类需要@Injectable注释 通过在构造函数中指定@Inject(InjectableClassName)参数,具有的类需要注入元素。 这些东西适用于所有类别

不过有一个警告,这可能是让你困惑的地方: 已通过angular以任何方式“注册”的类(意味着它们具有@Injectable、@Component、@Pipe或@Directive注释)具有其构造函数的快捷方式:它们不需要@Inject(..)参数,但该参数将通过指示类型和access关键字自动解析(和您一样,因为EventService具有@Injectable)。这只是一个“隐式”注入,angular将以相同的方式处理它

我认为这样说很重要,因为类型只是可能的“DI令牌”之一。 例如,我的一个项目中有一个构造函数:

    constructor(private eventService: EventService, @Inject(SERVICE_IMPLEMENTATIONS_MAP) private serviceMap: any) {}
正如您所看到的,serviceMap的类型是any,但我确实通过包含模块中提供的依赖项令牌注入了它,并且我实际上可以向它传递任何东西(我用于不同类型的模拟和没有公共API的实际服务)。正如您所看到的,我也为其他服务使用了快捷方式DI

记住,@Injectable并不意味着它的常量