アングル2のシングルトンサービスについて少し混乱しています。アプリケーション全体で翻訳サービスが必要です。サービスのインスタンスは1つだけです。私は言語を切り替える2つのボタンがあります。私は、アプリケーションを実行すると、私はコンテンツを翻訳するパイプを使用し、それは動作しますが、スイッチのボタンは動作しません。アングル2のシングルトンサービス
self.parent.context.use is not function 'というエラーメッセージが表示されます。
私はいくつかの角度の概念について誤解されているので、誰でも私が正しくグローバルサービスを実装するのを手伝ってくれますか? Angular2サービスで
//Translation service
@Injectable()
export class TranslateService {
private _currentLang: string;
constructor(){
this._currentLang = 'es';
}
public get currentLang() {
return this._currentLang;
}
public use(lang: string): void {
// set current language
this._currentLang = lang;
}
private translate(obj : LocalizedData): string {
// private perform translation
return obj[this._currentLang];
}
public instant(obj: LocalizedData) {
// call translation
return this.translate(obj);
}
}
//Navbar Module
@NgModule({
imports: [ CommonModule, FormsModule],
declarations: [ NavbarMenuComponent, TranslatePipe],
exports: \t \t [ NavbarMenuComponent]
})
export class NavbarModule { }
//App component
@Component({
selector: 'app',
template:'<navbar-menu ></navbar-menu>'
})
export class AppComponent implements OnInit{
public translatedText : string;
constructor(){}
ngOnInit(){}
}
//app Module
@NgModule({
imports: [ BrowserModule, FormsModule, NavbarModule],
declarations: [ AppComponent],
bootstrap: [ AppComponent],
providers: \t [ TranslateService],
})
export class AppModule { }
//Main
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule,[ TranslateService]);
デフォルトでは、すべてのサービスはangle2ではシングルトンです。ドキュメント「Singleton services」から。依存関係はインジェクタのスコープ内のシングルトンです。 – Igor
あなたは、正確にどこを見ているかを示す命令を提供するか、[最小限の、完全で検証可能な例を提供する](http://stackoverflow.com/help/mcve) )に焦点を当てて「Minimal」に焦点を当てた。上記のようなすべてのコードを提供すると、あなたの質問に多大な騒音が加わります。 – Igor