英文:
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 '@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 totalRecords = totalPosts ? totalPosts.toString() : undefined;
    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 });
  }
}
component:
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);
      });
  }
}
答案1
得分: 1
destroy通知可观察对象默认未定义。应该使用Subject定义它。
destroy = new Subject();
英文:
destroy notifier observable is undefined by default. It should be defined with Subject
destroy = new Subject();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论