如何编写触发/刷新HTTP POST方法的函数?

发布于 2025-02-11 02:00:03 字数 654 浏览 1 评论 0原文

如何在遥远的组件中编写一种方法,当示例性时,我单击一个按钮,此帖子方法订阅将再次运行?我认为,通过服务我应该到达这里...它(this.qwe)不应该在构造函数中仅仅是一个eaxample ...

  import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'post-request',
  templateUrl: 'post-request.component.html',
})
export class PostRequestComponent {
  postId;

  constructor(private http: HttpClient) {
      this.qwe()
  }
  

  qwe() {
    this.http
      .post<any>('https://reqres.in/api/posts', {
        title: 'Angular POST Request Example',
      })
      .subscribe((data) => {
        this.postId = data.id;
      });
  }
}

How can i write a method in far far away component,when forexample i click a button and this post method subscribe will run again? I think with a service i should get here...It(this.qwe) has not to be in constructor its just an eaxample...

  import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'post-request',
  templateUrl: 'post-request.component.html',
})
export class PostRequestComponent {
  postId;

  constructor(private http: HttpClient) {
      this.qwe()
  }
  

  qwe() {
    this.http
      .post<any>('https://reqres.in/api/posts', {
        title: 'Angular POST Request Example',
      })
      .subscribe((data) => {
        this.postId = data.id;
      });
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朕就是辣么酷 2025-02-18 02:00:03

如果您只是想提出一些帖子请求,请参阅第一部分,如果您实际需要调用其他组件的方法,请参阅第二部分。


恐怕您可能只是为了提出发布请求而制作整个组件 - 这不是必需的。您可以为此提供服务。

@Injectable({
  providedIn: 'root',
})
export class MyService {
  constructor(private http: HttpClient) {}

  post(): Observable<any> {
    return this.http.post<any>('https://reqres.in/api/posts', {
      title: 'Angular POST Request Example',
    });
  }
}

在任何组件中进行请求,

export class SomeComponent {
  constructor(private myService: MyService) {}

  qwe() {
    this.myService.post().subscribe((data) => {
      console.log(data);
    });
  }
}

然后,如果您希望其他组件调用此组件的方法,则 您可以为此组件创建服务。您可以将主题放在其中,该组件可以订阅,其他组件可以调用next() on。

@Injectable({
  providedIn: 'root',
})
export class PostRequestService {
  qweSubject = new Subject<void>();
}

在此组件中,我们可以订阅该主题,因此我们知道其他组件何时要调用该方法。确保在销毁组件时取消订阅以防止内存泄漏 - 这是必要的,因为该服务永远不会被破坏,因此受试者不会清理自己的订阅。

export class PostRequestComponent implements OnInit, OnDestroy {
  postId: any;
  qweSub = new Subscription();

  constructor(
    private http: HttpClient,
    private prService: PostRequestService
  ) {}

  ngOnInit(): void {
    this.qweSub = this.prService.qweSubject.subscribe(() => this.qwe());
  }

  qwe() { ... }

  ngOnDestroy() {
    this.qweSub.unsubscribe();
  }
}

在其他组件中,我们要调用该方法时注入服务并调用next()

export class SomeComponent {
  constructor(private prService: PostRequestService) {}

  qwe() {
    this.prService.qweSubject.next();
  }
}

If you're just trying to make some post requests see the first section, if you actually need to call another component's methods see the second section.


I'm afraid you might be making a whole component just to make post requests - which is not necessary. You can just make a service for this.

@Injectable({
  providedIn: 'root',
})
export class MyService {
  constructor(private http: HttpClient) {}

  post(): Observable<any> {
    return this.http.post<any>('https://reqres.in/api/posts', {
      title: 'Angular POST Request Example',
    });
  }
}

Then make the request in any component

export class SomeComponent {
  constructor(private myService: MyService) {}

  qwe() {
    this.myService.post().subscribe((data) => {
      console.log(data);
    });
  }
}

If you want other components to call this component's methods you can create a service for this component. You can put a Subject inside of it, which this component can subscribe to, and other components can call next() on.

@Injectable({
  providedIn: 'root',
})
export class PostRequestService {
  qweSubject = new Subject<void>();
}

In this component, we can subscribe to the subject so we know when other components want to call the method. Make sure to unsubscribe when the component is destroyed to prevent memory leaks - this is necessary because the service never gets destroyed, so the subject will not clean up its own subscriptions.

export class PostRequestComponent implements OnInit, OnDestroy {
  postId: any;
  qweSub = new Subscription();

  constructor(
    private http: HttpClient,
    private prService: PostRequestService
  ) {}

  ngOnInit(): void {
    this.qweSub = this.prService.qweSubject.subscribe(() => this.qwe());
  }

  qwe() { ... }

  ngOnDestroy() {
    this.qweSub.unsubscribe();
  }
}

And in other components, we inject the service and call next() when we want to call the method.

export class SomeComponent {
  constructor(private prService: PostRequestService) {}

  qwe() {
    this.prService.qweSubject.next();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文