Angular 如何从url中获取数据并将其保存在变量中
大家好,我正在尝试从URL获取数据并将数据保存在变量中,我需要该变量及其值 这是我的服务Angular 如何从url中获取数据并将其保存在变量中,angular,urlfetch,Angular,Urlfetch,大家好,我正在尝试从URL获取数据并将数据保存在变量中,我需要该变量及其值 这是我的服务 import {Injectable} from '@angular/core'; import {HttpClient, HttpErrorResponse} from "@angular/common/http"; const route: string = "http://localhost:51044/api/"; export enum userAc
import {Injectable} from '@angular/core';
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
const route: string = "http://localhost:51044/api/";
export enum userActions{
signin,
signup,
}
interface DataResponse {
result: boolean;
accessCode: string;
}
@Injectable({
providedIn: 'root'
})
export class TaskPlusPlusControllerService {
constructor(private http: HttpClient) {
}
public FetchData(properties: string, action: userActions)
{
switch (action)
{
case userActions.signin:
this.getListOfGroup('signin/' + properties);
break;
case userActions.signup:
break;
}
}
private getListOfGroup(url: string) {
let fetchedData: DataResponse = {result: null,accessCode: ''};
this.http.get(route + url).subscribe(
(res) => {
console.log(res);
console.log(res['result']);
console.log(res['accessCode']);
//fetchedData = res;
fetchedData.result = res['result'];
fetchedData.accessCode = res['accessCode'];
}
);
console.log(fetchedData);
}
}
因此,我直接从res
获取控制台中的数据,但是当我记录fetchedData
时,它的值没有改变
我应该怎么做呢?javascript是一种异步编程语言。您必须在subscribe方法中执行console.log操作
private getListOfGroup(url: string) {
let fetchedData: DataResponse = {result: null,accessCode: ''};
this.http.get(route + url).subscribe(
(res) => {
console.log(res);
console.log(res['result']);
console.log(res['accessCode']);
//fetchedData = res;
fetchedData.result = res['result'];
fetchedData.accessCode = res['accessCode'];
// move here
console.log(fetchedData);
}
);
// remove from here
console.log(fetchedData);
}
Javascript是一种单线程异步语言。调用承诺时,它不能是范围外的引用(只在{}内引用结果a承诺的值)。 如果要对另一个函数使用
fetchedData
。试试这个:
import {Injectable} from '@angular/core';
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
const route: string = "http://localhost:51044/api/";
export enum userActions{
signin,
signup,
}
interface DataResponse {
result: boolean;
accessCode: string;
}
@Injectable({
providedIn: 'root'
})
export class TaskPlusPlusControllerService {
fetchedData: DataResponse = null;
constructor(private http: HttpClient) {
this.fetchedData = {result: null,accessCode: ''};
}
public FetchData(properties: string, action: userActions)
{
switch (action)
{
case userActions.signin:
this.getListOfGroup('signin/' + properties);
break;
case userActions.signup:
break;
}
}
private getListOfGroup(url: string) {
let fetchedData: DataResponse = {result: null,accessCode: ''};
this.http.get(route + url).subscribe(
(res) => {
console.log(res);
console.log(res['result']);
console.log(res['accessCode']);
//fetchedData = res;
this.fetchedData.result = res['result'];
this.fetchedData.accessCode = res['accessCode'];
}
);
}
}
这里是答案顺便说一句,现在我可以在我的服务之外的组件中获取我的数据,如下所示
this.data = this.taskPlusPlusAPI.FetchData(this.phoneNumber,userActions.signin);
this.data.then(value => {
console.log(value);
})
http get请求是异步的,您在subscribe之外声明了一个变量,并且控制台也在subscribe之外记录它。。。尝试将console.log(fetchedData)放在订阅的末尾
this.data = this.taskPlusPlusAPI.FetchData(this.phoneNumber,userActions.signin);
this.data.then(value => {
console.log(value);
})