TypeScriptでグローバルオブジェクトを使用しようとしています。たとえば、リモート呼び出しが完了すると、ユーザーの詳細が入力されます。私はそれがアプリケーション全体で利用可能でバインド可能であることを望みます。誰かが例を指し示すことができますか?TypeScriptの共通オブジェクト
私はこのタイプのシングルトンクラスをユーザーの詳細クラスに実装しています。
ユーザーの詳細:
export class UserDetails {
//Gets populated for all the remote calls
userGuid: string;
firstName: string;
lastName: string;
fullName: string;
isAuthenticated: boolean;
private static _instance: UserDetails = new UserDetails();
constructor() {
if (UserDetails._instance) {
throw new Error("Error: Instantiation failed: Use UserDetails.User instead of new.");
}
UserDetails._instance = this;
}
public static get User(): UserDetails {
return UserDetails._instance;
}
@computedFrom('fullName')
get FullName(): string {
return this.fullName;
}
}
その他のページ:singleton:事前に
import {UserDetails} from 'models/userDetails';
export class Home {
/*@bindable*/router: Router;
//This does not have the values
//that gets populated after every remote call to the server
userDetails = UserDetails.User;
}
おかげ SenthilさんS
ここをクリックしてください:http://blog.eexit.net/js-singleton-as-module/ – Amid
@Amid - 私は自分のタイプのスクリプトクラスでやっていると思っています。この例はJavaスクリプトに書かれているので、同じタイプのスクリプトでは、私が望むように動作しません。何か不足していますか? –
正確にはありません。私のポストを答えとして見てください。 – Amid