简体   繁体   English

使用打字稿访问导入的服务时出现Angular2错误

[英]Angular2 error in accessing imported service using typescript

I am trying to create a form where the user will fill the required details and it would make a server call and save the required details. 我正在尝试创建一个表单,用户将在其中填写所需的详细信息,并进行服务器调用并保存所需的详细信息。 But I am getting this error no matter what I do. 但是无论我做什么我都会收到此错误。 ORIGINAL EXCEPTION: TypeError: this.postService is undefined My code for the main app.component.ts is : ORIGINAL EXCEPTION: TypeError: this.postService is undefined我主app.component.ts的代码是:

/// <reference path="../typings/tsd.d.ts" />
import {Component} from 'angular2/core';
import {HTTP_PROVIDERS} from 'angular2/http';
import {ControlGroup, Control, Validators, FormBuilder} from 
'angular2/common';
import {PostService} from './post.service'

@Component({
selector: 'my-app',
template:`

<form [ngFormModel]="form" (ngSubmit)="signup()">

<div class="form-group has-error has-feedback">  <div class="input-group">

            <input type="text" id="email_id" type="text" 
ngControl = "email_id" #email_id="ngForm" value = email_id >

        </div>  </div>
<div class="form-group has-error has-feedback"> <div class="input-group">

            <input type="password" id="password" type="text" 
ngControl = "password" #password="ngForm" value = password>

 </div>  </div>

<button class="btn btn-primary" type="submit">Sign Up</button>
</form>
`,
providers:[PostService, HTTP_PROVIDERS]


})

export class AppComponent {

form: ControlGroup;
postService:PostService;
constructor(fb: FormBuilder){
        this.form = fb.group({
            email_id: ['',Validators.required],
            password: ['',Validators.required]
        });     
}
signup(){
this.postService.createPost({ email: this.form.value.email_id,
 password: this.form.value.password });

}
}

I had earlier tried 'postService:PostService in the constructor but then also I was getting the same error that postService` is undifined. 我之前曾in the constructor but then also I was getting the same error that尝试过'postService:PostService in the constructor but then also I was getting the same error that未定义postService`。

the post.service.ts code is like : post.service.ts代码类似于:

import {Http} from 'angular2/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
import {Injectable} from 'angular2/core'
import {Post} from './post';
import {Observable} from 'rxjs/Observable';


@Injectable()
export class PostService {
//dependency injection
private _url = "http:127.0.0.1/accounts/login_user/";
constructor(private _http:Http) {

}


createPost(post:Post){

    return this._http.post(this._url,JSON.stringify(post))
    .map(res=>res.json());
}
}

How can I fix this issue and successfully send data from the form to the server and display the response. 如何解决此问题,并将数据成功地从表单发送到服务器并显示响应。 Can some one please help me out? 有人可以帮帮我吗?

Thank you. 谢谢。

You need to inject it into your component: 您需要将其注入到组件中:

export class AppComponent {
  form: ControlGroup;

  constructor(fb: FormBuilder, private postService:PostService){ <----
    (...)
  }

  (...)
}

Don't forget to specify this service in the providers of your component: 不要忘记在组件的提供程序中指定此服务:

@Component({
  providers: [ PostService ] 
})
export class AppComponent {
  (...)
}

or when bootstrapping the main component. 或引导主组件时。

bootstrap(AppComponent, [ PostService ]);

AppComponent ,需要注入服务:

constructor(fb: FormBuilder, this postService:PostService){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM