目前我正在尝试将某种设置类注入应用程序。
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
@Injectable()
export class Settings {
private apiUrl: string = '';
private location: string = '';
private language: string = 'en';
constructor() {
if(localStorage.getItem('apiUrl')) this.apiUrl = localStorage.getItem('apiUrl');
if(localStorage.getItem('location')) this.location = localStorage.getItem('location');
if(localStorage.getItem('language')) this.language = localStorage.getItem('language');
}
getApiUrl(): string {
return this.apiUrl;
}
getLocation(): string {
return this.location;
}
getLanguage(): string {
return this.language;
}
setApiUrl(apiUrl: string): void {
this.apiUrl = apiUrl;
localStorage.setItem('apiUrl', apiUrl);
}
setLocation(location: string): void {
this.location = location;
localStorage.setItem('location', location);
}
setLanguage(language: string): void {
this.language = language;
localStorage.setItem('language', language);
}
}
这是我的设置类...
我接下来尝试做的是通过整个应用程序注入(因为设置将被大量使用)。为此,我认为最好的方法是使用 bootstrap() 函数。所以这是我的引导功能:
bootstrap(AppComponent, [Settings])
我确实将它插入到我想使用 apiUrl 设置的服务的构造函数中:
@Injectable()
export class UserService {
constructor(private _settings: Settings, private _http:Http) { }
}
但是由于某种原因,在运行应用程序时,我在 UserService 中遇到错误,说构造函数的第一个参数(_settings)未定义..
我一直在尝试不同的事情,但还没有找到可行的解决方案..