【问题标题】:Property 'api' does not exist on type 'MyService'. any类型“MyService”上不存在属性“api”。任何
【发布时间】:2021-07-26 18:03:54
【问题描述】:

我正在关注一个教程,在该教程中我们创建了一个用于发送电子邮件的 Angular 服务 使用 Mailthis 电子邮件 API 的表单。 在服务代码中,“api”字样出现错误 “ 'MyService' 类型上不存在属性 'api'”。 任何建议都会非常有帮助!

我的代码是:

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

@Injectable({
  providedIn: 'root'
})
export class MyService {


    constructor(private http:HttpClient
              ) { }


    PostMessage(input:any){

        return this.http.post(this.api, input, { responseType:'text' }).pipe(
            map(
                (response:any) => {
                    if(response){
                        return response;
                    }
                },
                (error:any) => {
                    return error;
                }
            )
        )

    }


}

【问题讨论】:

  • 请始终发布包含此类问题的教程链接。错误很简单:MyService 是你自己的类,但它没有.api 属性。

标签: javascript angular typescript forms


【解决方案1】:

我没有看到您将 api 定义为变量的任何地方,我假设它是您要调用的端点,因此您可以将其定义为

api: string = "yourUrl";

【讨论】:

  • 感谢 Sajeetharan!,您说的非常对!非常感谢!!
猜你喜欢
  • 1970-01-01
  • 2017-01-29
  • 2021-10-10
  • 1970-01-01
  • 1970-01-01
  • 2021-01-09
  • 2019-08-16
  • 2021-07-10
相关资源
最近更新 更多