Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 公共变量can';不能由订阅设置_Angular_Ionic4_Angular7 - Fatal编程技术网

Angular 公共变量can';不能由订阅设置

Angular 公共变量can';不能由订阅设置,angular,ionic4,angular7,Angular,Ionic4,Angular7,这是一个带有Ionic 4框架的角度(7)项目,带有TypeScript。 我们需要从服务(user.service.ts)获取带有API调用的“userIdservice”变量 因此,当订阅执行响应时,他们不能设置“userIdservice”值。当我需要调用“this.userSevice.userIdservice”时,它们仍然是“未定义的” import { Injectable } from '@angular/core'; import { ApiService }

这是一个带有Ionic 4框架的角度(7)项目,带有TypeScript。 我们需要从服务(user.service.ts)获取带有API调用的“userIdservice”变量

因此,当订阅执行响应时,他们不能设置“userIdservice”值。当我需要调用“this.userSevice.userIdservice”时,它们仍然是“未定义的”

    import { Injectable } from '@angular/core';
    import { ApiService } from './api.service';
    import { CommonService } from './common.service';
    import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
    import { Observable } from 'rxjs';
    import { Router } from '@angular/router';

    @Injectable({
      providedIn: 'root'
    })
    export class UserService {

    public userIdservice:string;

    httpOptions = {
        headers: new HttpHeaders({
            'Content-Type':  'application/json'
      })
    };
    
    createUser(email, nome, password){
       this.http.post(this.api.url + '/users.json?access_token=' + xToken, 
        dataUser , this.httpOptions).subscribe(function(response: any){
       this.userIdservice = response.data.user.id;
       console.log("User id",  this.userIdservice); // Here in console i can see the ID
      },
      err => {console.log("Error"); },
      () => { ... another things... }
      );
    }
  }

我需要做什么才能从我的所有应用程序组件中获取具有响应API值的“userIdservice”?

错误

createUser(email, nome, password){
   this.http.post(this.api.url + '/users.json?access_token=' + xToken, 
    dataUser , this.httpOptions).subscribe((response: any) => {
   this.userIdservice = response.data.user.id;
   console.log("User id",  this.userIdservice); // Should work now
  },
  err => {console.log("Error"); },
  () => { ... another things... }
  );
}
您正在将回调函数实现为
function(){this.userIdservice='a'}
。使用此语法将丢失对此的引用

解决方案

改用箭头函数
()=>{this.userIdservice='a'}
将保留对此的引用

在您的代码中

createUser(email, nome, password){
   this.http.post(this.api.url + '/users.json?access_token=' + xToken, 
    dataUser , this.httpOptions).subscribe((response: any) => {
   this.userIdservice = response.data.user.id;
   console.log("User id",  this.userIdservice); // Should work now
  },
  err => {console.log("Error"); },
  () => { ... another things... }
  );
}

错误

createUser(email, nome, password){
   this.http.post(this.api.url + '/users.json?access_token=' + xToken, 
    dataUser , this.httpOptions).subscribe((response: any) => {
   this.userIdservice = response.data.user.id;
   console.log("User id",  this.userIdservice); // Should work now
  },
  err => {console.log("Error"); },
  () => { ... another things... }
  );
}
您正在将回调函数实现为
function(){this.userIdservice='a'}
。使用此语法将丢失对此的引用

解决方案

改用箭头函数
()=>{this.userIdservice='a'}
将保留对此的引用

在您的代码中

createUser(email, nome, password){
   this.http.post(this.api.url + '/users.json?access_token=' + xToken, 
    dataUser , this.httpOptions).subscribe((response: any) => {
   this.userIdservice = response.data.user.id;
   console.log("User id",  this.userIdservice); // Should work now
  },
  err => {console.log("Error"); },
  () => { ... another things... }
  );
}

考虑将请求代码移动到服务来处理执行调用。然后将这些调用的返回值设置为服务中的公共变量。这将使该服务的所有使用者都可以公开该值。从该服务返回的响应对象的数据是什么?@HojatAfsharan从响应中获取的数据是我在创建console.log时获取的“用户ID”的编号。但在外面我得到了“未定义的”。你的服务是如何提供的?您可能需要发布更多的代码,包括整个服务和模块代码,以便有人可以给您一个答案。@ HopyOne下面的代码是服务的恢复部分(用户。服务。TS),让我编辑它来改进。考虑将您的请求代码移动到一个服务来处理执行调用。然后将这些调用的返回值设置为服务中的公共变量。这将使该服务的所有使用者都可以公开该值。从该服务返回的响应对象的数据是什么?@HojatAfsharan从响应中获取的数据是我在创建console.log时获取的“用户ID”的编号。但在外面我得到了“未定义的”。你的服务是如何提供的?您可能需要发布更多的代码,包括整个服务和模块代码,以便有人能给您答案。@希望下面的代码是服务的简历部分(user.service.ts),让我对其进行编辑以改进。