Angular 5组件期待一个参数

时间:2018-01-06 19:13:53

标签: javascript angular typescript

我正在尝试一个简单的个人资料应用程序,突然间我得到错误TS2554

/app/components/profile/profile.component.ts(25,3)中的错误:错误TS2554:预期有1个参数,但得到0。

import { Component, OnInit } from '@angular/core';
import { AuthService } from '../../services/auth.service';
import { FlashMessagesService } from 'angular2-flash-messages';
import { Router } from '@angular/router';

@Component({
  selector: 'app-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {

    user: Object;

    constructor(
        private auth: AuthService,
        private flashMsg: FlashMessagesService,
        private router: Router
        ) {
    }

    ngOnInit() {

        this.auth.getProfile().subscribe( profile => {
            this.user = profile.user;
        },
        err => {
            return false;
        });

    }

}

auth.service.ts

import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
import { tokenNotExpired } from 'angular2-jwt';

@Injectable()
export class AuthService {

    authToken: any;
    user: any;

    constructor(
        private http: Http
        ) {
    }



    getProfile(user) {
        let headers = new Headers();
        this.loadToken();
        headers.append('Authorization', this.authToken);
        headers.append('Content-Type','application/json');
        return this.http.get('http://localhost:3000/users/profile', {headers:headers})
            .map(res => res.json());
    }

    loadToken() {
        const token = localStorage.getItem('id_token');
        this.authToken = token;
    }
}

1 个答案:

答案 0 :(得分:1)

您的 getProfile 期待名为 user 的参数,但您没有从组件中传递

您需要传递一个参数,如下所示,

  this.auth.getProfile(user).subscribe( profile => {
            this.user = profile.user;
  },
  err => {
            return false;
  });

或者如果您不需要参数,请将其从服务方法中删除。