(Angular) 如何将API Get响应输出到变量中

huangapple go评论62阅读模式
英文:

(Angular) How do I output an API Get response into a variable

问题

新手使用Angular,目前正在开发一个小项目。我有一个本地服务器在运行,我打算从中获取一个名称,然后将其写入一个变量(repeat),以在我的HTML页面上使用。我想知道是否有人有任何建议,关于如何最好地将我的GET请求的响应写入一个变量中,并(请原谅我的无知,因为我是面向对象编程的新手,但在我的AppComponent类中引用这个变量)。谢谢。

import { Injectable, Input } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { AppComponent } from './app.component';

@Injectable({
  providedIn: 'root'
})

export class sqlservice {
  constructor(public http: HttpClient) { }
  ngOnInit(){
  this.http.get('http://localhost:7271/sql').subscribe(Response) => {
    // 在这里将响应赋值给你的变量,例如:
    // AppComponent.repeat = Response;
  }
}

}
import { Component } from '@angular/core';
import { repeat } from 'rxjs';
import { sqlservice } from './app.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  title = 'angulartest';
  repeat = ''; // 在这里定义你的变量
}
英文:

New to Angular, and currently working on a small project. I have a local server running and I intend to pull a name down from it, then write it to a variable (repeat) to be used on my html page. I was wondering if anyone had any suggestions as to how best to write my get requests response to a variable, and (excuse my ignorance as I am new to object oriented programming, but reference this variable in my AppComponent class). Thank you.

import { Injectable, Input } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { AppComponent } from './app.component';

@Injectable({
  providedIn: 'root'
})


export class sqlservice {
  constructor(public http: HttpClient) { }
  ngOnInit(){
  this.http.get('http://localhost:7271/sql').subscribe(Response)=>
}

}

import { Component } from '@angular/core';
import { repeat } from 'rxjs';
import { sqlservice } from './app.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']

})


export class AppComponent {
title= 'angulartest';
repeat=;

}

答案1

得分: 1

欢迎来到 Angular!我明白你的问题:

要在你的 AppComponent 类中使用来自 HTTP GET 请求的响应,你需要在你的服务中创建一个返回 Observable 的方法,然后在你的 AppComponent 中订阅这个 Observable。

你可以这样做:

export class SqlService {
  constructor(private http: HttpClient) { }
  
  getData(): Observable<any> {
    return this.http.get('http://localhost:7271/sql');
  }
}

在你的服务类中不需要 ngOnInit,因为你将在你的 AppComponent 中调用 getData(),而这个组件已经有 ngOnInit。

在 AppComponent 中,你需要做一些事情:

就像你在服务中调用了 HttpClient 一样,在你的 AppComponent 构造函数中现在需要调用 SqlService。其次,你需要初始化 getData。最终的结果将看起来像这样:

export class AppComponent implements OnInit {
  title = 'angulartest';
  data: any; // 这个变量将保存来自 HTTP GET 请求的响应数据

  constructor(private sqlService: SqlService) { }

  ngOnInit() {
    this.sqlService.getData().subscribe(response => {
      this.data = response;
      // 现在你可以在 AppComponent 中访问数据
      console.log(this.data);
    });
  }
}

这里有详细的文档,深入介绍了与后端服务通信的内容。

希望这能帮助你,如果有任何问题,请在此回答中留下评论!

英文:

Welcome to Angular! I got you:

In order to use the response from the HTTP get request in your AppComponent class, you would want to have a method in your service that returns an Observable, and then subscribe to that Observable in your AppComponent.

You could do it like this:

export class SqlService {
  constructor(private http: HttpClient) { }
  
  getData(): Observable&lt;any&gt; {
    return this.http.get(&#39;http://localhost:7271/sql&#39;);
  }
}

No need for ngOnInit in your service class, because you're going to call getData() in your AppComponent which has the ngOnInit.

In AppComponent, you'll want to do a couple things:

Just like you called the HttpClient in your service, you'll want to now call that SqlService in the constructor of your AppComponent. Second, you'll want to initialize getData. The end result will look something like this:

export class AppComponent implements OnInit {
  title = &#39;angulartest&#39;;
  data: any; // This variable will hold the response from the HTTP GET request

  constructor(private sqlService: SqlService) { }

  ngOnInit() {
    this.sqlService.getData().subscribe(response =&gt; {
      this.data = response;
      // You now have access to the data within your AppComponent
      console.log(this.data);
    });
  }
}

Here is solid documentation that goes into deep detail about communicating with backend services.

Hope this helps, add a comment to this answer if you have any questions!

huangapple
  • 本文由 发表于 2023年7月27日 23:04:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定