我一直在尝试设置Angular 4应用程序。我在网络前端工作方面已经多年了,所以我可能会做一些明显愚蠢的事情。帮助将不胜感激。
我有一个使用注入令牌的配置对象。我有两个使用它的服务。一个参数只需一个:
constructor(@Inject(APP_CONFIG) private config: AppConfig) {
this.requestUrl = this.config.restRoot + listPresentationsUrl;
}
进一步开发了另一项服务,并使用HttpClient从服务器提取数据。但是构造函数:
constructor (@Inject(APP_CONFIG) private config: AppConfig, private web: HttpClient) {
this.requestUrl = config.restRoot + listDecksUrl;
}
导致我不期望的错误。组件使用此服务来获取数据。只要我在上面的构造函数中使用这两个参数,我就会收到no provider for [object Object]!
错误。触发它的组件是使用该服务的组件。如果我使用两个参数中的一个,则应用程序不会触发错误。
有什么建议吗?完整的类在下面 - 我从onInit中删除了一些代码,但它对错误没有影响(这是我触发错误的最小代码)。
组件(decklist.component.ts)
import { Component, OnInit } from '@angular/core';
import { DecksService } from './decks.service';
import { Deck } from './deck.model';
@Component({
selector: 'slides-decks',
templateUrl: './decklist.component.html',
styleUrls: ['./decklist.component.css']
})
export class DeckListComponent implements OnInit {
presentations: Deck[];
constructor(private deckService: DecksService) {}
ngOnInit(): void {
}
}
服务(decks.service.ts)
import { Injectable, Inject } from '@angular/core';
import { Deck } from './deck.model';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/do';
import { AppConfig, APP_CONFIG, SLIDESHOW_CONFIG } from './../shared/app.config';
const listDecksUrl: string = 'list-decks';
@Injectable()
export class DecksService {
private requestUrl: string;
constructor (@Inject(APP_CONFIG) private config: AppConfig, private http: HttpClient) {
this.requestUrl = this.config.restRoot + listDecksUrl;
}
listDecks(): Observable<Deck[]> {
return this.http.get<Deck[]>(this.requestUrl);
}
}
共享应用配置(app.config.ts)
import { InjectionToken } from '@angular/core';
export let APP_CONFIG = new InjectionToken<AppConfig>('app.config');
export interface AppConfig {
username: string;
password: string;
restRoot: string;
}
export const SLIDESHOW_CONFIG: AppConfig = {
username: '**********',
password: '**********',
restRoot: 'http://localhost:8070/api'
};
错误是:
错误:没有[object Object]的提供者! at injectionError(core.es5.js:1169) 在noProviderError(core.es5.js:1207) 在ReflectiveInjector_.webpackJsonp ... / .. / .. / core/@angular/core.es5.js.ReflectiveInjector _。 throwOrNull(core.es5.js:2649) 在ReflectiveInjector .webpackJsonp ... / .. / .. / core/@angular/core.es5.js.ReflectiveInjector _。 getByKeyDefault(core.es5.js:2688) 在ReflectiveInjector .webpackJsonp ... / .. / .. / core/@angular/core.es5.js.ReflectiveInjector _。 getByKey(core.es5.js:2620) 在ReflectiveInjector .webpackJsonp ... / .. / .. / core/@angular/core.es5.js.ReflectiveInjector_.get(core.es5.js:2489) at resolveNgModuleDep(core.es5.js:9481) at _callFactory(core.es5.js:9556) at _createProviderInstance $ 1(core.es5.js:9495) at resolveNgModuleDep(core.es5.js:9477)
答案 0 :(得分:0)
为了记录,这是一个完全不同模块的拼写错误。 Angular有时会出现意外错误。