大佬教程收集整理的这篇文章主要介绍了如何从 url 中以 angular 的形式获取数据并将其保存在变量中,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
大家好,我正在尝试从 URL 获取数据并将该数据保存在一个变量中,我需要该变量及其值。
这是我的服务。
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 是一种异步编程语言。您必须在订阅方法中执行 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 是一种单线程异步语言。当您调用 promise 时,它不能在范围之外被引用(您只能在 {} 内引用 result a promise 的值)。
如果您想将 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'];
}
);
}
}
,
import {InjectablE} from '@angular/core';
import {httpClient} from "@angular/common/http";
const route: String = "http://localhost:51044/api/";
export enum userActions{
signin,}
@Injectable({
providedIn: 'root'
})
export class TaskPlusPlusControllerservice {
blockedPhonenumbers: String[] = [];
fetchedData = null;
constructor(private http: httpClient) {
}
/*
private handleError(error: httpErrorResponsE) {
if (error.error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it accordingly.
console.log("An error occurred:",error.error.messagE);
} else {
// The BACkend returned an unsuccessful response code. The response body may contain clues as to what went wrong,console.log(
`BACkend returned code ${error.status},` + `body was: ${error.error}`
);
}
// return an observable with a user-facing error message
return throwError(error);
}
private extractData(res: ResponsE) {
return res || {};
}
*/
public FetchData(properties: String,action: userActions) {
switch (action) {
case userActions.signin:
/* if (this.blockedPhonenumbers.length != 0) {
return this.getListOfGroup('signin/' + properties);
} else {
for (let item in this.blockedPhonenumbers) {
if (item == properties) {
return null;
}
}
return this.getListOfGroup('signin/' + properties);
}*/
return this.getListOfGroup('signin/' + properties);
break;
case userActions.signup:
break;
}
}
private async getListOfGroup(url: String) {
this.fetchedData = await this.http.get(route + url).toPromise();
return this.fetchedData;
}
}
这里是答案顺便说一句,现在我可以像这样在组件中从我的服务中获取我的数据
this.data = this.taskPlusPlusAPI.FetchData(this.phonenumber,userActions.signin);
this.data.then(value => {
console.log(value);
})
以上是大佬教程为你收集整理的如何从 url 中以 angular 的形式获取数据并将其保存在变量中全部内容,希望文章能够帮你解决如何从 url 中以 angular 的形式获取数据并将其保存在变量中所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。