我正在尝试从我的 api 返回信息,但我不明白如何正确使用订阅。使用数组时,我从我的服务返回一个空数组,并在获取值时将其推送到其中。
我如何在 app-component ts 中正确地只返回一个值变量。现在如果我执行 alert(JSON.stringify(authenticated))
它只会给我 {"_isUnsubscribed":false,"_subscriptions":[{"isUnsubscribed":false}]}
应用组件 ts
checkAuthentication(){
var authenticated = this._authService.getAuthenication();
}
认证服务
getAuthenication() {
return this._http.get('auth.php').subscribe(res => {
if (res.json().authenticated === true){
return true;
}
return false;
});
}
auth.php
<?
session_start();
$authenticated = ( isset($_SESSION["authenticated"]) ? true : false );
$post_data = json_encode(array(authenticated => $authenticated));
echo $post_data;
?>
最佳答案
改变
return this._http.get('auth.php').subscribe(res => {
到
return this._http.get('auth.php').map(res => {
并称它为
this._authService.getAuthenication()
.subscribe(value => this.authenticated = value);
关于javascript - TypeScript Angular 2 响应订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35524520/