无法使用Angular 15进行HTTP请求

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

Can't make http request with Angular 15

问题

由于某种原因,我无法让我的Angular 15应用程序发送HTTP请求。我习惯了旧版本的Angular,所以我想知道是否有一些我遗漏或其他配置。我已经在app.module.ts的imports中添加了HttpClientModule。我知道不是端点的问题,因为我在Postman的服务中使用完全相同的URL,它可以正常访问。我缺少什么。感谢任何帮助!

服务:

import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class MainFeedService {
  baseUrl: string = 'http://localhost:3000';
  constructor(private http: HttpClient) {}

  getMainFeed(userId: string) {
    const param = new HttpParams().set('userId', userId);

    const params = param;
    return this.http.get<{
      records: any;
      totalPosts: number;
    }>('http://localhost:3000/local/main-feed', { params });
  }
}

组件:

import { Component } from '@angular/core';
import { takeUntil } from 'rxjs';
import { MainFeedService } from 'src/services/trades.service';

@Component({
  selector: 'app-main-feed',
  templateUrl: './main-feed.component.html',
  styleUrls: ['./main-feed.component.scss'],
})
export class MainFeedComponent {
  destroy: any;
  totalPosts = 0;
  userId = 'test';
  constructor(private mainFeedService: MainFeedService) {}

  ngOnInit() {
    this.mainFeedService
      .getMainFeed(this.userId)
      .pipe(takeUntil(this.destroy))
      .subscribe((res: any) => {
        console.log('RES', res);
      });
  }
}
英文:

For some reason I can't get my Angular 15 app to send an http request. I'm used to an older version of Angular so I wonder if it's some configuration I'm missing or something. I also already added HttpClientModule under imports in app.module.ts. I know it's not the endpoint because I use that exact same url in my service in postman and it hits it no problem.
What am I missing. I appreciate any help!

service:

import { HttpClient, HttpParams } from &#39;@angular/common/http&#39;;
import { Injectable } from &#39;@angular/core&#39;;

@Injectable({
  providedIn: &#39;root&#39;,
})
export class MainFeedService {
  baseUrl: string = &#39;http://localhost:3000&#39;;
  constructor(private http: HttpClient) {}

  getMainFeed(userId: string) {
    // const totalRecords = totalPosts ? totalPosts.toString() : undefined;
    const param = new HttpParams().set(&#39;userId&#39;, userId);

    const params = param;
    return this.http.get&lt;{
      records: any;
      totalPosts: number;
    }&gt;(&#39;http://localhost:3000/local/main-feed&#39;, { params });
  }
}

component:

import { Component } from &#39;@angular/core&#39;;
import { takeUntil } from &#39;rxjs&#39;;
import { MainFeedService } from &#39;src/services/trades.service&#39;;

@Component({
  selector: &#39;app-main-feed&#39;,
  templateUrl: &#39;./main-feed.component.html&#39;,
  styleUrls: [&#39;./main-feed.component.scss&#39;],
})
export class MainFeedComponent {
  destroy: any;
  totalPosts = 0;
  userId = &#39;test&#39;;
  constructor(private mainFeedService: MainFeedService) {}

  ngOnInit() {
    this.mainFeedService
      .getMainFeed(this.userId)
      .pipe(takeUntil(this.destroy))
      .subscribe((res: any) =&gt; {
        console.log(&#39;RES&#39;, res);
      });
  }
}

答案1

得分: 1

destroy通知可观察对象默认未定义。应该使用Subject定义它。

destroy = new Subject();
英文:

destroy notifier observable is undefined by default. It should be defined with Subject

destroy = new Subject();

huangapple
  • 本文由 发表于 2023年6月22日 08:55:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528027.html
匿名

发表评论

匿名网友

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

确定