无法将数据从组件传递到服务

时间:2016-10-22 11:05:32

标签: angular typescript

我正在尝试将参数传递给服务中定义的函数。参数始终为protected static function getFacadeAccessor() { return 'auth.password'; }

login.component.ts

undefined

authentication.service.ts

import { Component, OnInit } from '@angular/core';

import { AuthenticationService } from '../authentication.service';

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

credentials: {
    username: '',
    password: ''
};

constructor(private _authenticationService: AuthenticationService) {}

ngOnInit() { }

login() {

    this._authenticationService.login(this.credentials).subscribe(
        response => console.log(response),
        error => console.log(error),
        () => console.log('Done.')
    );

}

我错过了什么?

1 个答案:

答案 0 :(得分:2)

您正在完美地传递数据,但问题是您声明了类型为credentials的变量{ username: '', password: '' };,您没有为其赋值,这就是undefined的原因。 :为变量指定类型,=指定值。这就是你要找的东西:

credentials: any = {
    username: '',
    password: ''
};