Angular2服务方法不可访问

Angular 2 service method is not accessible

我有以下文件并试图访问服务方法 loginMethod。请帮忙,我在这个错误上浪费了我 2 天的时间。我是新来的 Angular 2.

app.module

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router';
import { HttpModule } from '@angular/http';



import { AppComponent } from './app.component';
import { LoginComponent } from './Login/login.component';

import { LoginService } from './Login/login.service';


import 'rxjs/Rx'; //Load all features

@NgModule({
    imports: [BrowserModule, FormsModule, HttpModule],
    declarations: [AppComponent, LoginComponent],
    providers: [LoginService],
    bootstrap: [AppComponent]
})

export class AppModule { }

app.component

import { Component } from '@angular/core'; 
import { LoginComponent } from './Login/login.Component';

@Component({
    selector: 'pm-app',
    template: 

`<div>
                <h1>Success</h1>
                <pm-login></pm-login>
                </div>`
})

export class AppComponent {
}

login.component

import { Component, OnInit } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
import { LoginService } from './login.service';

// Observable class extensions
import 'rxjs/add/observable/of';
// Observable operators
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';

@Component({
    selector: 'pm-login',
    template: `<div>
                    <h1>Login</h1>
                    <input type="text" placeholder="User Name"/><br/>
                    <input type="Password" placeholder="Password"/><br/>
                    <input type="button" title="Login" value="Login" (click)='loginClick()' />
                </div>`,
    providers: [LoginService]
})

export class LoginComponent {
    constructor() { }

    private _loginService: LoginService;
    private _loginRes: boolean;

    loginClick() {
        alert("login component");
        this._loginService.loginMethod().subscribe(resLogin => this._loginRes = resLogin);
    }

    ngOnInit() {
        this.loginClick();
        this._loginService.alertMethod();
    }
}

login.service

import { Http, Response, Headers } from '@angular/http'
import { Observable } from 'rxjs/Observable'
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';

@Injectable()
export class LoginService {
    constructor()
    { }
    private _http: Http;
    private _apiUrl: string = 'http://localhost:13131/api/Account/Get';
    private headers = new Headers({ 'Content-Type': 'application/json' });

    alertMethod()
    {
        alert("alert Method");
    }


    loginMethod()  {
        alert('Login Service');
        return this._http.get(this._apiUrl)
            .map((response: Response) => response.json()).catch(this.handleError);

    }

    private handleError(error: any): Promise<any> {
        console.error('An error occurred', error); // for demo purposes only
        return Promise.reject(error.message || error);
    }
}

将您的代码更改为此;

export class LoginComponent {
    constructor(private _loginService: LoginService) { }

    private _loginRes: boolean;

还要改变这个

export class LoginService {
    constructor(private _http: Http)
    { }

这就是 DI 的工作原理。