Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 订阅';不存在于类型';承诺<;用户>';角度6_Javascript_Angular_Angular6 - Fatal编程技术网

Javascript 订阅';不存在于类型';承诺<;用户>';角度6

Javascript 订阅';不存在于类型';承诺<;用户>';角度6,javascript,angular,angular6,Javascript,Angular,Angular6,从angular 4到angular 6代码,现在我在vs代码中得到一个错误,即属性“subscribe”在类型“Promise”上不存在。错误在Profile.component.ts中。不知道是什么问题 下面是代码 用户类 export class User { id: number name: string email: string avatar: string joined: string } import { Component, OnInit } f

从angular 4到angular 6代码,现在我在vs代码中得到一个错误,即属性“subscribe”在类型“Promise”上不存在。错误在Profile.component.ts中。不知道是什么问题

下面是代码

用户类

export class User {
   id: number
   name: string
   email: string
   avatar: string
   joined: string
}
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router'
import { UserService } from '../services/user.service'
import { User } from '../classes/User'

@Component({
  selector: 'app-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {
  id: number
  user: User
  constructor(private router: ActivatedRoute, private userService: UserService) { }

  ngOnInit() {
    this.router.params.subscribe((param) => {
      this.id = +param['id']
    })

    this.userService.getUserById(this.id)
      .subscribe((user) => this.user = user)
  }

}
用户服务

import { Injectable } from '@angular/core'
import { AuthService } from './auth.service'
import { Http, Headers, RequestOptions } from '@angular/http';
import { CONFIG } from '../config/config'
import { User } from '../classes/User'
import 'rxjs'


    @Injectable()

    export class UserService {
        private headers: Headers
        constructor(private authService: AuthService, private http: Http) {
            this.headers = new Headers({ 'Authorization': `Bearer ${this.authService.getToken()}` })
        }

        getUserById(id: number): Promise<User> {
            if (id == this.authService.getAuthUserId()) {
                return Promise.resolve(this.authService.getAuthUser())
            }

            let options = new RequestOptions({ headers: this.headers })
            this.http.get(`${CONFIG.API_URL}/user/${id}`, options)
                .subscribe((response) => {
                    console.log(response.json())
                })
        }
    }

错误消息自解释。您正在尝试访问
Promise
subscribe
功能,该功能不可用

您应该使用
然后
而不是
订阅

  this.userService.getUserById(this.id)
      .then((user) => this.user = user)
  }
注意:您可能还需要更改
getUserById
的实现。因为它看起来不正确

getUserById(id: number): Promise<any> {
            const p = new Promise<string>((resolve, reject) =>  {
            if (id == this.authService.getAuthUserId()) {
                return resolve(this.authService.getAuthUser())
            }
            let options = new RequestOptions({ headers: this.headers })
            this.http.get(`${CONFIG.API_URL}/user/${id}`, options)
                .subscribe((response) => {
                    resolve(response.json())
                })

      });
      return p;

  }
getUserById(id:number):承诺{
const p=新承诺((解决、拒绝)=>{
如果(id==this.authService.getAuthUserId()){
返回解析(this.authService.getAuthUser())
}
let options=new RequestOptions({headers:this.headers})
this.http.get(`${CONFIG.API_URL}/user/${id}`,选项)
.订阅((响应)=>{
解析(response.json())
})
});
返回p;
}
注意:因为代码是直接在stackoverflow编辑器中编写的。可能有打字错误或语法错误。请改正一下


如果将其更改为“then”,则它只会在控制台中显示一个错误。通过安慰TypeError:无法读取Profile.component.ts中未定义的属性'then'。请在
getUserById
函数中检查答案。下面出现在“return p”处。类型'Promise'不可分配给类型'Promise'。类型“字符串”不能分配给类型“用户”。请将
Promise
更改为
Promise
。谢谢您,先生。它起作用了:-)你能解释一下确切的错误是什么吗?