【问题标题】:Property 'subscribe' does not exist on type 'void' in angular-cliangular-cli 中的“void”类型上不存在属性“subscribe”
【发布时间】:2022-05-07 20:43:44
【问题描述】:

我是 Angular 2 的新手,对于发布的类似问题没有找到任何合适的答案。

我在 angular-cli 中的“void”类型上不存在属性“subscribe”。我尝试从 rxjs 导入订阅,但没有找到该库。

Error Image

请帮助解决这个问题。请在下面找到我正在调用该服务的服务和组件代码。

// ******************************* 

// login.service.ts

// ******************************* 

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/throw';

@Injectable()
export class LoginService {

  constructor(private http: Http) { }

  getLoginData(){
    this.http.get('./json/login.json')
      .map((result: Response) => result.json())
      .catch(this.getError);
  }

  private getError(error: Response): Observable<any>{
      console.log(error);
      return Observable.throw(error.json() || 'Server Issue');
  }

}

// ******************************* 

// login.component.ts

// ******************************* 


import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { LoginService } from '../login.service';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css'],
  providers: [LoginService]
})
export class LoginComponent {
usernameModel: string;
passwordModel: string;
validLogin: Boolean;
  constructor(private router: Router, private loginService: LoginService) { }

  homeNav(){
    if(this.usernameModel === 'jay' && this.passwordModel === 'Jay'){
      this.validLogin = true;
      this.router.navigate(['/home']);
    }
    else{
      this.validLogin = false;
      console.log('Login Error');
    }

this.loginService.getLoginData()
  .subscribe(
    data => console.log(data)
  );

  }



}

【问题讨论】:

    标签: angular


    【解决方案1】:

    您没有在 getLoginData 函数中返回 observable。添加 return 关键字并可选择将返回类型添加到签名中:

    getLoginData(): Observable<any>{
        return this.http.get('./json/login.json')
          .map((result: Response) => result.json())
          .catch(this.getError);
      }
    

    【讨论】:

      【解决方案2】:

      您应该返回 http 调用,并确保始终添加类型提示以让您的 IDE 提前发现这些错误

      getLoginData(): Observable<any> {
        return this.http.get('./json/login.json')
          .map((result: Response) => result.json())
          .catch(this.getError);
      }
      

      【讨论】:

      • 谢谢。现在我发现新问题为“找不到模块:错误:无法解析 'rxjs/add/operator/throw'' .... 我检查了 rxjs/operator 文件夹,但找不到 throw.js ... . 我需要在外部添加它吗?
      • 正确的导入是import 'rxjs/add/observable/throw'(如此可观察,而不是操作员)。但那些东西是不必要的,我相信你唯一需要的就是 map 导入
      【解决方案3】:

      我正在使用 Angular 13

      [Post.services.ts]

      API 上的简单使用返回

      postProduct(数据:任意){

      return this.http.post<any>(" http://localhost:3000/productList/", data)
      

      }

      [Post.component.ts]

      addProduct(){

      if(this.productForm.valid){
      
        this.API_DailogService.postProduct(this.productForm.value)
        .subscribe({
          next:()=>{
            alert("Product added successfully!")
          },
          error: () =>{
            alert("Error while adding product!")
          }
        })
      
      }
      

      }

      [Post.component.html]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-22
        • 2022-06-28
        • 2017-10-09
        • 1970-01-01
        • 2022-07-06
        • 2017-10-20
        • 1970-01-01
        相关资源
        最近更新 更多