未捕获的错误:无法解决角度为

时间:2018-03-29 11:43:38

标签: node.js angular

我正在使用Angular,Nodejs和MongoDB进行简单的用户注册。

我无法将表单数据从angular发布到Nodejs以继续进行。

app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { HttpModule } from '@angular/http';
import { Http, Response, RequestOptions, Headers } from '@angular/http';


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

export class AppModule { }

app.component.html:

<form #registerForm="ngForm" (ngSubmit)="onSubmit(registerForm)">

<div class="form-group">
    <label for="name">Name</label>
    <input type="text" class="form-control" name="name" ngModel>
</div>

<div class="form-group">
    <label for="email">Email</label>
    <input type="email" class="form-control" name="email" ngModel>
</div>

<div class="form-group">
     <label for="password">Password</label>
     <input type="password" class="form-control" name="password" ngModel>
</div>

<div class="form-group">
      <button type="submit">Submit</button>
</div>

</form>

app.component.ts:

import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormControl, FormArray } from 
   '@angular/forms';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
    constructor(private http:Http) { } 
    onSubmit(registerForm) {
        console.log(registerForm.value);
        let url = 'http://localhost:8080/signup';
        this.http.post(url, {registerForm(registerForm)}).subscribe(res => 
           console.log(res.json()));
    }
}

节点(routes.js):

app.post('/signup', passport.authenticate('local-signup', {
    successRedirect : '/login', 
    failureRedirect : '/signup',
    failureFlash : true 
}));

节点服务器正在8080端口上运行。

当我尝试发布数据时,我在浏览器控制台中收到以下错误:

未捕获错误:无法解析AppComponent的所有参数:(?)。

有人可以帮忙解决这个问题吗?知道如何继续下去也很棒。

1 个答案:

答案 0 :(得分:1)

您忘记将http导入app.component。您已在构造函数中声明private http:Http。因此,import { Http } from '@angular/http';添加app.component

使用HttpClient代替遗留Http,如下所示。