ここに私のコードはありますか?データを返すhttpプロバイダ関数を呼び出すsetUserDetails関数によって変数 'user'にデータを割り当てようとしています。fetchUserDetails予想通り。 getUserDetailsはヌル値を出力します。イオン2 - 角2の他の関数によってhttpプロバイダによって割り当てられた値にアクセスできない
@Injectable()
export class UserDetailsProvider {
public userChanged = new EventEmitter();
private userUrl = apiUrl+'get_user_details_api'; // URL to web api
public user : any;
constructor(public http: Http, public authService: AuthServiceProvider, public events: Events) {
this.user = null;
}// end of constructor
fetchUserDetails(){
return new Promise((resolve, reject) => {
let headers = new Headers();
headers.append('jwt', localStorage.getItem('token'));
this.http.post(apiUrl+'get_user_details_api', {}, {headers: headers})
.subscribe(res => {
resolve(res.json());
}, (err) => {
reject(err);
});
});
}// end of get user details
setUserDetails(){
this.fetchUserDetails().then((result) => {
this.user = result;
console.log(result); // Added this as requested, I told you it's working
this.events.publish('UserLogged', this.user);
});
}// end of function
getUserDetails(){
//console.log(this.user); commenting this out to show the log of http then result
}// end of function
イベントはthis.userを公開し、appコンポーネントからアクセスできますが、それでも問題ありません。 seUserDetailsが呼び出されたときにログイン時に一度ユーザーデータを設定し、this.userを返すことでgetUserDetailsを呼び出すだけでどこでもユーザーデータにアクセスできますが、アクセスできません。あなたのためのコンソールの出力があり
...
Angular is running in the development mode. Call enableProdMode() to enable the production mode.
VM17281 main.js:60356 Native: tried calling SplashScreen.hide, but Cordova is not available. Make sure to include cordova.js or run in a device/simulator
VM17281 main.js:33868 Object {user_id: "MEM17042246C9FE2C039", name: "aryan", is_active: "1", is_phone_verified: "1", email: "[email protected]"…}email: "[email protected]"ifile_name: "1705051493994229.jpg"ipath: "uploads/MEM17042246C9FE2C039 /profile_pic"is_active: "1"is_phone_verified: "1"login_count: "0"name: "aryan"user_id: "MEM17042246C9FE2C039"user_type: "3"__proto__: Object
'setUserDetails'関数の' then'に 'console.log(result)'を追加できますか?ちょうどあなたがいくつかのrsultsを取得するかどうかを確認する(私はそれが動作しない場合はないと仮定します) – trichetriche
はい私はそれを試みた...それは動作しています。 seUserDetailsのconsole.log(this.user)でも動作します。私はすでにイベントを発表しました。パブリッシュは働いています。 –
このサービス、モジュールレベルまたはコンポーネントレベルはどのように提供しましたか? – Alex