javascript - Angular 5 组件需要一个参数

标签 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;
    }
}

最佳答案

您的 getProfile 需要一个名为 user 的参数,但您没有从组件传递它

你需要传递一个参数如下,

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

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

关于javascript - Angular 5 组件需要一个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48130922/

相关文章:

javascript - 如何实现滚动条

javascript - 忽略行 Gulp-Uncss

angular - 以 Angular 6 从 html 导出 Pdf

Angular2 调整输入图像的大小

typescript - ts-node-dev:使用 dotenv 时没有提供要运行的脚本

javascript - 错误 : Uncaught (in promise): Failed to load login. component.html

javascript - jquery 域名和 url 验证

angular - Ionic 3 网格无法将行项目垂直居中

angular - 三人组 : Draw elbow with begin radius and end radius

javascript - 为什么我的 event.currentTarget 会自动更改?