【问题标题】:Property 'subscribe' does not exist on type 'Promise<Response类型“Promise<Response”上不存在属性“订阅”
【发布时间】:2018-12-02 08:10:29
【问题描述】:

我正在向 api 发出 http 请求,然后想使用 .subscribe 来控制结果,但由于类型“Promise”上不存在属性“subscribe”而面临此问题。

我的 Angular 版本是 6.2,我的 Typescript 版本是 2.9。

我的 AuthService 文件在这里

import { Injectable } from '@angular/core'
import { CONFIG } from '../config/config'
import { Http } from '@angular/http'
import 'rxjs/add/operator/toPromise'


@Injectable()

export class AuthService {

    constructor(private http: Http) {

    }

    register(name: String, email: String, password: String) {
        this.getConfig()
            .subscribe((response => {
                console.log(response)
            }))
    }

    getConfig() {
        return this.http.post(`${CONFIG.API_URL}/register`, { name: name, email: email, password: password }).toPromise()
    }
}

【问题讨论】:

    标签: angular typescript angular6


    【解决方案1】:

    只需删除 toPromise()

     getConfig() {
            return this.http.post(`${CONFIG.API_URL}/register`, { name: name, email: email, password: password })
        }
    

    【讨论】:

    • 它有效 :-) 我认为在 Angular 6 中它是默认导入的?不是吗?
    猜你喜欢
    • 2018-02-01
    • 2019-01-03
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 2018-12-02
    • 1970-01-01
    相关资源
    最近更新 更多