无法访问Angular 2服务方法

时间:2017-03-31 12:44:40

标签: angular angular2-services angular2-components angular2-http

我有以下文件并尝试访问服务方法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);
    }
}

enter image description here

1 个答案:

答案 0 :(得分:2)

将您的代码更改为此内容;

#include <stdio.h>
struct book
    {
        char bookID[20];
        char name[20];
        double price;
    };

void input(struct book bs[], int n)
{
    for (int i = 0; i < n; i++)
    {
    printf("Please input the price of book %s:\n",bs[i].name );
    scanf("%f",&bs[i].price);
    }
}

void print(struct book bs[], int n)
{
    for (int i = 0; i < n; i++)
    {
        printf("%s\t%s\t%.2f\n", bs[i].bookID, bs[i].name, bs[i].price );
    }
}

int main(void)
{
    book books[4] = {{"0101","Computer",1.5},{"0102","Programming",4.1},{"0103","Math",3.3},{"0104","English",1.2}};    
    input(books, 4);
    print(books, 4);
    return 0;
}

并且还改变了这个

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

    private _loginRes: boolean;

这就是DI的工作方式。