【问题标题】:Uncaught Error: Can't resolve all parameters for AppComponent: (?) in angular未捕获的错误:无法解析 AppComponent 的所有参数:(?)角度
【发布时间】:2018-03-29 11:43:37
【问题描述】:

我正在使用 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 的所有参数:(?)。

谁能帮忙解决这个问题?知道之后如何进行也会很棒。

【问题讨论】:

    标签: node.js angular


    【解决方案1】:

    您忘记将http 导入到您的app.component。您已经在构造函数中声明了private http:Http。所以将import { Http } from '@angular/http'; 添加到您的app.component

    并使用HttpClient 代替旧版Http,如下所述。

    【讨论】:

    • 您也不应该再使用 HTTP,因为它已被弃用。应该使用 HttpClient (angular.io/guide/http)
    • 是的,我同意@Shadowlauch。
    • 感谢@Shadowlauch。但是我收到了这个错误:“在这一行找不到名称 Http '{' 预期”- this.http.post(url, {registerForm(registerForm)}).subscribe(res => console.log(res.json( )));
    • 现在运行良好。谢谢你 Efe 和 Shadowlauch。
    猜你喜欢
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 2018-12-18
    • 2023-03-16
    • 2019-12-31
    • 1970-01-01
    相关资源
    最近更新 更多