簡體   English   中英

當我在請求中“返回”時,“無效”類型上不存在屬性“訂閱”

[英]Property 'subscribe' does not exist on type 'void' when I 'return' in the request

任務.service.ts:

import { Injectable } from '@angular/core';
import { WebRequestService } from './web-request.service';

@Injectable({
  providedIn: 'root',
})
export class TaskService {
  constructor(private webReqService: WebRequestService) {}

  createList(title: string) {
    return this.webReqService.post('lists', { title });
  }

  getLists() {
    return this.webReqService.get('lists');
  }
}

任務-view.component.ts:

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { TaskService } from 'src/app/task.service';

@Component({
  selector: 'app-task-view',
  templateUrl: './task-view.component.html',
  styleUrls: ['./task-view.component.scss'],
})
export class TaskViewComponent implements OnInit {
  lists: any[];

  constructor(
    private taskService: TaskService,
    private route: ActivatedRoute
  ) {}

  ngOnInit() {
    this.route.params.subscribe((params: Params) => {
      console.log();
    });

    this.taskService.getLists().subscribe((lists: any[]) => {
      this.lists = lists;
    });
  }
}

我在 task.service.ts 行(23)上收到錯誤: this.taskService.getLists().subscribe((lists: any[]) => {

錯誤 TS2339:“void”類型上不存在屬性“subscribe”。

我知道有很多關於“無效”類型的“訂閱”屬性不存在的問題,但它們似乎都可以通過在請求中添加“返回”來解決;

但是,我沒有忘記 getLists() 方法中的“返回”。 我很困惑為什么在有返回時它被接受為 void 方法,以及如何解決這個錯誤。

必須添加返回到 WebRequestService.get

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

@Injectable({
  providedIn: 'root',
})
export class WebRequestService {
  readonly ROOT_URL;

  constructor(private http: HttpClient) {
    this.ROOT_URL = 'http://localhost:3000';
  }

  get(uri: string) {
    return this.http.get(`${this.ROOT_URL}/${uri}`);
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM